PostsController#中的ActiveRecord :: RecordNotFound显示单击链接

  • 错误

     ActiveRecord::RecordNotFound in PostsController#show Couldn't find Post with id=morecommented Request Parameters: {"id"=>"morecommented"} 

    我在哪里做错了?

    postscontroller #show action

     def show  @post = Post.find(params[:id])  ...  end 

    morecommented.html.erb

       '/posts', :action => 'show', :id => t.id %>

    耙路线

     post GET /posts/:id(.:format) {:action=>"show", :controller=>"posts"} .... posts_morecommented /posts/morecommented(.:format) {:controller=>"posts", :action=>"morecommented"} 

    routes.rb中:

      resources :posts match "posts/:id/categ" => "posts#categ" match "posts/:id/tag_posts" => "posts#tag_posts" match "posts/searcharchive" => "posts#searcharchive" match "posts/morecommented" => "posts#morecommented" 

    在资源调用之前移动匹配

     match "posts/morecommented" => "posts#morecommented" resources :posts 

    或者你也可以

     resources :posts do get :morecommented, on: :collection end 

    你的问题在你的routes.rb文件里面,因为路由匹配从上到下的动作posts/morecommented匹配posts/:id动作与params[:id]等于morecommented如Gerry提到的一个解决方案是改变顺序并移动在调用resources :posts之前match "posts/morecommented" => "posts#morecommented" resources :posts routes.rb文件中的resources :posts另一个是在posts/:id route中设置要求posts/:id