无法为删除创建 button_to
Can't created button_to for deleting
我有资源:
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)...
在 rails4 中使用 resources
http://guides.rubyonrails.org/routing.html#crud-verbs-and-actions
时肯定是 destroy
方法而不是 delete
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?' } %>
我有资源:
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)...
在 rails4 中使用 resources
http://guides.rubyonrails.org/routing.html#crud-verbs-and-actions
destroy
方法而不是 delete
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?' } %>