Rails link_to destroy a nested resource?

独自空忆成欢 提交于 2020-01-02 00:47:13

问题


I have a nested resource attachments and I want to create a link_to to destroy/delete the attachment. Here's what I have, but it is posting as a GET versus a PUT:

<%= link_to "Delete Attachment", project_thread_attachment_path(@attachment.thread.project.id, @attachment.thread.id, @attachment.id), :confirm => "Are you sure you want to delete this attachment?", :method => :delete, :action => "destroy" %>

Ideas? Thanks!


回答1:


You should be able to use the following by itself (remove the :action => 'destroy' part). Also, the request should be a DELETE request, not a PUT request:

<%= link_to "Delete Attachment", project_thread_attachment_path(@attachment.thread.project.id, @attachment.thread.id, @attachment.id), :confirm => "Are you sure you want to delete this attachment?", :method => :delete %>



回答2:


Try

link_to "Delete Attachment", [@attachment.thread.project,@attachment.thread,@attachment], :confirm => "Are you sure?", :method => :delete

Does it work?



来源:https://stackoverflow.com/questions/4940481/rails-link-to-destroy-a-nested-resource

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!