resources :posts
/app/controllers/posts_controller.rb:@H_403_5@
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
我是新手,我经常对路线感到困惑.我有另一个static_pages控制器.在home.html.erb文件中.@H_403_5@
我想做的是致电 – @H_403_5@
def categoryshow @post = Post.find(params[:category]) end
来自/app/views/static_pages/home.html.erb的帖子控制器的’categoryshow’方法@H_403_5@
我该如何管理?如果我使用’posts_path’,它将转到索引操作而不是categoryshow操作.@H_403_5@
#@H_403_5@
我通读了这个链接并尝试了一些东西.这是我面临的问题:@H_403_5@
当我在config / routes.rb中尝试这个时@H_403_5@
resources :posts do collection do get 'categoryshow' end end
这会生成’categoryshow_posts_path’@H_403_5@
在我看来,我使用了这个:@H_403_5@
<ul class="users"> <%= Post::CATEGORIES.each do |category| %> <li> <%= link_to category,categoryshow_posts_path %> </li> <% end %> </ul>
def categoryshow@H_403_5@
@post = Post.find(params[:category])
结束@H_403_5@
PostsController #categyshow中的ActiveRecord :: RecordNotFound@H_403_5@
无法找到没有ID的帖子@H_403_5@
其次,我尝试使用您提供的链接中提到的非资源路由:@H_403_5@
match ':posts(/:categoryshow(/:category))'
在视图中,我使用这个:@H_403_5@
<ul class="users"> <%= Post::CATEGORIES.each do |category| %> <li> <%= link_to category,"posts/#{category}" %> </li> <% end %> </ul>
在这种情况下,只有当没有其他现有的资源路由匹配时,我们的非资源路由才会匹配.但是,我看到显示操作匹配,我收到此错误消息:@H_403_5@
这是show动作:@H_403_5@
def show@H_403_5@
@post = Post.find(params[:id])
结束@H_403_5@
PostsController#show中的ActiveRecord :: RecordNotFound@H_403_5@
找不到具有id = Politics的帖子@H_403_5@
>我真的很感激这里的任何帮助!!
>谢谢(Sidharth)@H_403_5@