问题
<li><%= link_to('More Commented', posts_morecommented_path) %></li>
Error
ActiveRecord::RecordNotFound in PostsController#show
Couldn't find Post with id=morecommented
Request
Parameters:
{"id"=>"morecommented"}
Where am I doing the mistake?
postscontroller#show action
def show @post = Post.find(params[:id]) ... end
morecommented.html.erb
<% @moreCommented.each do |t| %>
<%= link_to t.title, :controller => '/posts', :action => 'show', :id => t.id %><br/>
<% end %>
rake routes
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"
回答1:
move the match before the resources call
match "posts/morecommented" => "posts#morecommented"
resources :posts
Alternatively you can do
resources :posts do
get :morecommented, on: :collection
end
回答2:
Your problem is inside your routes.rb file since routes are matched from the top to the bottom action posts/morecommented matches posts/:id action with params[:id] equal to morecommented One solution as Gerry mentioned is to change order and move the match "posts/morecommented" => "posts#morecommented" before the call resources :posts in your routes.rb file the other one is to set requirements on :id in your posts/:id route.
来源:https://stackoverflow.com/questions/8506969/activerecordrecordnotfound-in-postscontrollershow-clicking-a-link