【发布时间】:2015-10-25 12:52:22
【问题描述】:
在我的项目中有 2 个嵌套资源:
Rails.application.routes.draw do
resources :posts do
resources :comments
end
root 'posts#index'
end
我正在使用部分 _comment.html.erb 渲染一组 cmets
<%= render partial: "comments/comment", collection: @post.comments %>
部分看起来像这样
<div class="comment_wrapper">
<p class="comment_name"><%= comment.name %></p>
<p class="comment_date"><%= comment.created_at %></p>
<p class="comment_body"><%= comment.body %></p>
<%= link_to "Delete comment", post_comment_path(@post.id, id: comment.id), method: :delete%>
</div>
问题在于“删除评论”链接中的嵌套路由。
我一直无法传递:id 密钥。我尝试了几种不同的方法来传递链接中的变量,但不断收到相同的错误,即缺少 :id 键。当我用段落替换链接以显示comment.id 时,它会完美显示,因此在我看来它绝对可用。
No route matches {:action=>"show", :controller=>"comments", :format=>nil, :id=>nil, :post_id=>11} missing required keys: [:id]
如您所见,它还尝试调用“显示”操作,但我敢打赌,只要它正确传递了 id,就会解决这个问题。 有什么想法我可能在这里做错了吗?
【问题讨论】:
标签: ruby-on-rails routes nested