为什么 Rails 会使用“link_to”不适用于删除操作?
在 index.html.erb
中,我显示所有产品,并且在每个产品旁边有 Edit
和 Delete
操作:
<% @products.each do |product| %>
...
<%= link_to("Edit", edit_product_path(product.id), :class => 'action') %>
<%= link_to("Delete", product, :method => :delete, :class => 'action') %>
...
<% end %>
Edit< /code> 链接工作正常。但是,
Delete
链接不起作用。我收到以下错误:
Unknown action
The action 'show' could not be found for ProductsController
我猜这是因为请求方法是 GET 而不是 DELETE。但是,我不知道如果我显式设置 :method => 为什么会发生这种情况:删除
。
routes.rb
非常简单:
root :to => "products#index"
resources :products
我启用了 Javascript。
请建议。
In index.html.erb
I display all products, and next to each product I have Edit
and Delete
actions:
<% @products.each do |product| %>
...
<%= link_to("Edit", edit_product_path(product.id), :class => 'action') %>
<%= link_to("Delete", product, :method => :delete, :class => 'action') %>
...
<% end %>
The Edit
link works ok. However, the Delete
link does not work. I get the following error:
Unknown action
The action 'show' could not be found for ProductsController
I guess it is because the request method is GET rather than DELETE. But, I don't know why this happens if I set explicitly :method => :delete
.
routes.rb
is pretty simple:
root :to => "products#index"
resources :products
I have Javascript enabled.
Please suggest.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不要忘记在您的
application.js
文件中包含jquery_ujs
:Dont forget to include
jquery_ujs
in yourapplication.js
file:在删除链接中,它需要是
product_path(product)
而不是product
。It needs to be
product_path(product)
instead ofproduct
in your delete link.我遇到了同样的问题 - 实际上我已将旧的“删除”操作更改为“销毁” - 但忘记了如果您使用 SSL..(例如 ssl_required :destroy)
I had same problem - actually I had changed my old 'delete' action to 'destroy' - but forgot If your using SSL.. (e.g ssl_required :destroy)
您是否在
javascript_include_tag
中指定了rails.js
?这是不引人注目的DELETE
方法正常工作所必需的。如果您使用 jQuery,那么也有一个解决方案。Do you have
rails.js
specified in ajavascript_include_tag
? This is required for the unobtrusiveDELETE
method to work. If you're using jQuery then there's a solution for that too.