Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 由于nil值导致路由错误_Ruby On Rails_Ruby_Ruby On Rails 3 - Fatal编程技术网

Ruby on rails 由于nil值导致路由错误

Ruby on rails 由于nil值导致路由错误,ruby-on-rails,ruby,ruby-on-rails-3,Ruby On Rails,Ruby,Ruby On Rails 3,我在运行Rail应用程序时遇到以下错误。我认为这是因为Rails显示的是0而不是1的注释。记录0不存在。我想做的就是编辑每条评论 我认为错误在于我如何创建到嵌套资源的链接(即链接到来自帖子的评论) 错误 “没有路由匹配{:action=>”show“,:controller=>”comments“,:post_id=>1,:id=>nil} 请尝试运行rake routes以获取有关可用路由的详细信息。” show.html.erb: <% @post.comments.each do |

我在运行Rail应用程序时遇到以下错误。我认为这是因为Rails显示的是0而不是1的注释。记录0不存在。我想做的就是编辑每条评论

我认为错误在于我如何创建到嵌套资源的链接(即链接到来自帖子的评论)

错误

“没有路由匹配{:action=>”show“,:controller=>”comments“,:post_id=>1,:id=>nil} 请尝试运行rake routes以获取有关可用路由的详细信息。”

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.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 %>
下面的代码现在起作用了。我需要在链接中传递id值

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

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

“你确定吗?”,:method=>:delete%>

请尝试使用此代码生成链接

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

甚至

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


请添加您的
$rake routes
输出。“我认为这是因为Rails显示的是0而不是1的注释”这是错误的假设
@post.comments
返回注释对象的集合,因此当您使用
每个
迭代该集合时,您将在每个循环中获得一个注释对象。它与数组中的项目位置无关。这似乎有效。“c”返回注释id号。