我有资源:
resources :articles
出于某种原因,我无法找到方法删除的路径。我需要为button_to
执行此操作。
#from pry
Rails.application.routes.url_helpers.articles_path("432432", {method: :delete}) # => "/articles/432432?method=delete"
Rails.application.routes.url_helpers.articles_path(id: "432432", method: :delete)
=> "/articles/432432?method=delete"
Rails.application.routes.url_helpers.articles_path({ controller: :articles, action: :delete, id: "432432"})
=> "/articles?action=delete&id=432432"
# and so on...
按钮同样如此:
<%= button_to 'Destroy', { controller: :articles, action: :delete, id: 'some_id' }, method: :delete, data: { confirm: 'Are you sure?' } %>
它说
No route matches {:controller=>\"articles\", :action=>\"delete\", :id=>\"43433\"}
为什么呢?我想我没有使用正确的论据或混淆他们的订单。
注意我使用的是我从某处获得的自定义ID,因此我无法执行此操作:
articles_path(@article)...
答案 0 :(得分:0)
在使用destroy
http://guides.rubyonrails.org/routing.html#crud-verbs-and-actions
delete
方法,而不是resources
在rails3中也是如此:http://guides.rubyonrails.org/v3.2.13/routing.html#crud-verbs-and-actions
所以这应该有效:
<%= button_to 'Destroy', { controller: :articles, action: :destroy, id: 'some_id' }, method: :delete, data: { confirm: 'Are you sure?' } %>