Ruby on rails Can';t创建用于删除的按钮\u
我有以下资源:Ruby on rails Can';t创建用于删除的按钮\u,ruby-on-rails,ruby,ruby-on-rails-3,Ruby On Rails,Ruby,Ruby On Rails 3,我有以下资源: 资源:文章 由于某种原因,我找不到方法delete的路径。我需要为按钮_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"
资源:文章
由于某种原因,我找不到方法delete的路径。我需要为按钮_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?' } %>
在rails4中,当使用
资源时,它肯定是销毁
方法,而不是删除
rails3也是如此:
因此,这应该是可行的:
<%= button_to 'Destroy', { controller: :articles, action: :destroy, id: 'some_id' }, method: :delete, data: { confirm: 'Are you sure?' } %>
即使方法的真实名称是delete?控制器操作被称为destroy
delete
指的是HTTP方法delete。如果您在控制器中实现了一个delete函数,并且想要使用它,您必须为它定义一个路由<代码>参考资料:文章
仅定义指南中规定的路线。使用按钮中的action::destroy
将调用继承的destroy
函数,而不是您的delete
函数。
<%= button_to 'Destroy', { controller: :articles, action: :destroy, id: 'some_id' }, method: :delete, data: { confirm: 'Are you sure?' } %>