1
<li><%= link_to('More Commented', posts_morecommented_path) %></li> 

错误的ActiveRecord :: RecordNotFound在PostsController#显示单击链接

ActiveRecord::RecordNotFound in PostsController#show 

Couldn't find Post with id=morecommented 

Request 

Parameters: 

{"id"=>"morecommented"} 

我在哪里做的错误?

postscontroller#show动作

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 %> 

耙路线

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" 
+0

你迁移的数据库? –

+0

添加您的'PostsController#show'动作代码和'routes.rb'或'rake routes'输出 – Bohdan

+0

@MarkW,我为什么要现在迁移数据库?你什么意思 ? – shibly

回答

5
资源调用

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

移动匹配或者你可以做

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

你的问题是你routes.rb文件中,因为路线是从顶部匹配底部行动posts/morecommented比赛posts/:id行动params[:id]等于morecommented一个解决方案为格里提到的是改变秩序和移动电话resources :postsmatch "posts/morecommented" => "posts#morecommented"routes.rb另一个是在你的posts/:id路线上设置:id的要求。

+0

非常感谢Bohdan。 – shibly

相关问题