Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 670
Misha Moroshko Avatar asked Dec 15 '10 04:12

Misha Moroshko


1 Answers

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.

like image 122
Ryan Bigg Avatar answered Sep 30 '22 12:09

Ryan Bigg