Rails link_to销毁嵌套资源?

AnA*_*ice 15 ruby-on-rails ruby-on-rails-3

我有一个嵌套的资源附件,我想创建一个link_to销毁/删除附件.这就是我所拥有的,但它是作为GET而不是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" %>
Run Code Online (Sandbox Code Playgroud)

想法?谢谢!

Ger*_*rry 16

尝试

link_to "Delete Attachment", [@attachment.thread.project,@attachment.thread,@attachment], :confirm => "Are you sure?", :method => :delete
Run Code Online (Sandbox Code Playgroud)

它有用吗?

  • 这对我很有用,你能解释一下原因吗?为什么括号而不是foo_path(parent_of_foo,foo)? (2认同)
  • @TJSherrill括号是嵌套记录的数组,然后由Rails"处理"以计算所有给定嵌套记录的路径(根据您的路由).在这种情况下,数组的第一个元素是Project,第二个是Thread,第三个是Attachment.Rails"猜测"一个Attachment属于一个属于一个Project的Thread,用这些嵌套记录计算一个路径:`project`,然后是`thread`,然后是`attachment`,它导致了helper:`project_thread_attachment_path` (2认同)

Pan*_*kos 8

您应该能够自己使用以下内容(删除:action =>'destroy'部分).此外,请求应该是DELETE请求,而不是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 %>