【问题标题】:Routing Error due to nil value由于 nil 值导致的路由错误
【发布时间】:2012-09-18 20:22:05
【问题描述】:

运行 Rail App 时出现以下错误。我相信这是因为 Rails 从 0 开始显示 cmets,而不是 1。记录 0 不存在。我想做的就是让编辑每条评论成为可能。

我认为错误在于我创建嵌套资源链接的方式(即从帖子链接到 cmets)。

错误

"没有路由匹配 {:action=>"show", :controller=>"cmets", :post_id=>1, :id=>nil} 尝试运行 rake 路由以获取有关可用路由的更多信息。”

show.html.erb:

<% @post.comments.each do |c| %>
<p>
  <b><%=h c.name %> said:</b><br />
  <%= c.created_at %>
</p>

<p>
  <%=h c.body %>
</p>

<p>
  <%=h c.id %>
</p>

<%= link_to 'test', post_comment_path(:post_id => @post.id, :id => c.id) %> |

<%#= link_to 'Edit', edit_post_comment_path(:id => @comment.id,
                                           :id => @post.id) %>

<%= link_to 'Comment', [@post, :comments ] %> |
<%= link_to 'Edit', edit_comment_path(@comment) %> |
<%= link_to 'Back', comments_path %>

当我删除这一行时,错误就消失了。

@post.id, :id => c.id) %> |

耙路线输出:

   C:\RUBY\RailsInstaller\Ruby1.9.3\bin\ruby.exe -e  $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift)
    C:\RUBY\RailsInstaller\Ruby1.9.3\bin\rake routes
          posts_list GET    /posts/list(.:format)                       posts#list
       post_comments GET    /posts/:post_id/comments(.:format)          comments#index
                     POST   /posts/:post_id/comments(.:format)          comments#create
    new_post_comment GET    /posts/:post_id/comments/new(.:format)      comments#new
   edit_post_comment GET    /posts/:post_id/comments/:id/edit(.:format) comments#edit
        post_comment GET    /posts/:post_id/comments/:id(.:format)      comments#show
                     PUT    /posts/:post_id/comments/:id(.:format)      comments#update
                     DELETE /posts/:post_id/comments/:id(.:format)             comments#destroy
               posts GET    /posts(.:format)                            posts#index
                     POST   /posts(.:format)                            posts#create
            new_post GET    /posts/new(.:format)                        posts#new
           edit_post GET    /posts/:id/edit(.:format)                   posts#edit
                post GET    /posts/:id(.:format)                        posts#show
                     PUT    /posts/:id(.:format)                        posts#update
                     DELETE /posts/:id(.:format)                        posts#destroy

   Process finished with exit code 0

更新:

以下现在有效。我需要在链接中传递 id 值。

<%= link_to 'Edit', edit_post_comment_path(@post.id, c) %>

<%= link_to 'Destroy', [c.post, c], :confirm => 'Are you sure?', :method => :delete %>

【问题讨论】:

  • 请加你$ rake routes输出。
  • "我相信这是因为 Rails 显示的 cmets 从 0 开始,而不是 1" 这是错误的假设。 @post.comments 返回评论对象的集合,因此当您使用 each 遍历该集合时,您会在每个循环中获得一个评论对象。它与数组中的项目位置无关。

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


【解决方案1】:

试试这个代码生成链接

<%= link_to 'test', post_comment_path(@post, c) %>

甚至

<%= link_to 'test', [@post, c] %>

【讨论】:

  • 这似乎有效。 "c" 带回 cmets id 号。
猜你喜欢
  • 2014-07-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多