【问题标题】:ActiveRecord::RecordNotFound in PostsController#show clicking a linkActiveRecord::RecordNotFound in PostsController#show 点击链接
【发布时间】:2011-12-14 15:20:48
【问题描述】:
<li><%= link_to('More Commented', posts_morecommented_path) %></li>

错误

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

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

【问题讨论】:

  • 添加您的PostsController#show 操作代码和routes.rbrake routes 输出
  • @MarkW,为什么我现在要迁移数据库?什么意思?
  • @Bohdan,我已经更新了原来的帖子/问题。
  • 在您的 routes.rb 文件中将 match "posts/morecommented" =&gt; "posts#morecommented" 移动到调用 resources :posts 之前
  • @Gerry,再次查看原帖。

标签: ruby-on-rails ruby ruby-on-rails-3 routing ruby-on-rails-3.1


【解决方案1】:

在资源调用之前移动匹配

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 Gerry 提到的一种解决方案是更改顺序和在routes.rb 文件中将match "posts/morecommented" =&gt; "posts#morecommented" 移动到调用resources :posts 之前,另一种是在posts/:id 路由中对:id 设置要求。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多