Why Rails "link_to" does not work for delete action?

Misha Moroshko picture Misha Moroshko · Dec 15, 2010 · Viewed 10.8k times · Source

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.

Answer

Ryan Bigg picture Ryan Bigg · Dec 15, 2010

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.