我的资源组织如下:
resources :tickets do
resources :comments
end
当我尝试使用此类链接从故障单中删除评论时(列出所有评论):
<table class="table">
<% @ticket.comments.each do |c| %>
</tr>
<td><%= c.text %> | <%= link_to "Delete", ticket_comment_path(c), method: :delete, data: {confirm: "Are you sure?"} %></td>
</tr>
<% end %>
</table>
我有一个错误: 没有路由匹配{:action =&gt;“show”,:controller =&gt;“comments”,:id =&gt;“5”}缺少必需的密钥:[:ticket_id]
正如我想的那样,因为ticket_comment_path(c)id应该是注释的id,而且应该填充ticket_id。
但不知怎的,我的:id是一个票证ID,并且:ticket_id是空的......
答案 0 :(得分:1)
尝试ticket_comment_path(@ticket, c)
- 嵌套资源需要两个ID才能正确路由。您可以通过运行rake routes | grep comment
来查看路线,并且您会看到DELETE /tickets/:ticket_id/comments/:id
答案 1 :(得分:1)
当您使用嵌套资源时,网址将如下所示
/tickets/:ticket_id/comments/:id
因此,要删除评论,您需要传递ticker_id
和comment_id
两个参数。您的删除link_to
应如下所示
<%= link_to "Delete", ticket_comment_path(@ticket.id, c), method: :delete, data: {confirm: "Are you sure?"} %>
来源:http://guides.rubyonrails.org/routing.html#nested-resources