ActiveRecord::RecordNotFound in PostsController#show clicking a link

£可爱£侵袭症+ 提交于 2020-01-05 09:12:50

问题


<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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!