为什么 Rails 需要“link_to"对删除操作不起作用? [英] Why Rails "link_to" does not work for delete action?
问题描述
在 index.html.erb
中我显示所有产品,在每个产品旁边我有 Edit
和 Delete
操作:
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 %>
Edit
链接工作正常.但是,Delete
链接不起作用.我收到以下错误:
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
我猜是因为请求方法是GET而不是DELETE.但是,如果我明确设置 :method =>,我不知道为什么会发生这种情况.:删除
.
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
非常简单:
root :to => "products#index"
resources :products
我启用了 Javascript.
I have Javascript enabled.
请提出建议.
推荐答案
您是否在 javascript_include_tag
中指定了 rails.js
?这是不显眼的 DELETE
方法工作所必需的.如果您使用 jQuery,那么还有一个 解决方案.
Do you have rails.js
specified in a javascript_include_tag
? This is required for the unobtrusive DELETE
method to work. If you're using jQuery then there's a solution for that too.
这篇关于为什么 Rails 需要“link_to"对删除操作不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!