问题描述
我有一个控制器名称帖子.在我的 /config/routes.rb
中,我使用了这个 -
I have a controller name posts. In my /config/routes.rb
, I have used this -
resources :posts
/app/controllers/posts_controller.rb:
class PostsController < ApplicationController
def new
@post = Post.new
end
def show
@post = Post.find(params[:id])
end
def categoryshow
@post = Post.find(params[:category])
end
def index
@posts = Post.all
end
def create
@post = Post.new(params[:post])
if @post.save
flash.now[:success] = "Your Post Successful"
redirect_to @post
else
render 'new'
end
end
end
我是 Rails 的新手,我经常对路线感到困惑.我有另一个 static_pages 控制器.其中有一个 home.html.erb
文件.
I am new to rails and I often get confused with routes. I have another static_pages controller. In there is a home.html.erb
file.
我想做的是打电话-
def categoryshow
@post = Post.find(params[:category])
end
来自/app/views/static_pages/home.html.erb
我该如何处理?如果我使用posts_path",它会转到索引操作而不是 categoryshow 操作.
How do I manage that? If I use 'posts_path', it goes to index action instead of categoryshow action.
我通读了链接并从那里尝试了一些东西.这是我面临的问题:
I read through the link and tried a couple of things from there. Here is the problem that I am facing:
当我在 config/routes.rb 中尝试这个时
When I tried this in the config/routes.rb
resources :posts do
collection do
get 'categoryshow'
end
end
这会生成一个categoryshow_posts_path"
This generates a 'categoryshow_posts_path'
在我看来,我使用了这个:
In my view, I used this:
<ul class="users">
<%= Post::CATEGORIES.each do |category| %>
<li>
<%= link_to category,categoryshow_posts_path %>
</li>
<% end %>
</ul>
我的帖子控制器有以下方法:
My posts controller has the following method:
定义类别显示
@post = Post.find(params[:category])
结束
在这种情况下,我收到以下错误:
In this case, I am getting the following error:
ActiveRecord::RecordNotFound in PostsController#categoryshow
ActiveRecord::RecordNotFound in PostsController#categoryshow
无法找到没有 ID 的帖子
Couldn't find Post without an ID
其次,我尝试使用您提供的链接中提到的非资源丰富的路由:
Secondly, I tried out using non resourceful routes as mentioned in that link you provided:
match ':posts(/:categoryshow(/:category))'
在视图中,我使用的是这个:
In the view, I am using this:
类别
<ul class="users">
<%= Post::CATEGORIES.each do |category| %>
<li>
<%= link_to category,"posts/#{category}" %>
</li>
<% end %>
</ul>
在这种情况下,我们的非资源路由只有在没有其他现有资源路由匹配时才会匹配.但是,我看到显示操作已匹配,并且收到此错误消息:
In this case, our non resourceful route will match only if no other existing resourceful route matches. However, i am seeing that show action is matched and I get this error message:
这是表演动作:
定义显示
@post = Post.find(params[:id])
结束
ActiveRecord::RecordNotFound in PostsController#show
ActiveRecord::RecordNotFound in PostsController#show
找不到 id=Politics 的帖子
Couldn't find Post with id=Politics
我真的很感谢这里的任何帮助!!
I'd really appreciate any help here!!
谢谢(Sidharth)
Thanks (Sidharth)
推荐答案
resources :posts do
collection do
get 'categoryshow'
end
end
或者:
resources :posts do
get 'categoryshow', :on => :collection
end
之后的部分讨论了在不满足您的确切需求的情况下添加任意路由.
The section after that discusses adding arbitrary routes if that doesn't meet your exact needs.
请注意,路由文件是明确排序的:如果您在其他东西捕获它之后尝试匹配它,则第一个路由获胜.
Note that the routes file is explicitly ordered: if you try to match a route after something else would have captured it, the first route wins.
这篇关于Ruby on rails 从视图路由到控制器中的自定义方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!