Misha Moroshko
Misha Moroshko

Reputation: 171321

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.

Upvotes: 9

Views: 10949

Answers (4)

Benjamin Crouzier
Benjamin Crouzier

Reputation: 41855

Dont forget to include jquery_ujs in your application.js file:

//
//= require jquery
//= require jquery_ujs
// ...

Upvotes: 5

John
John

Reputation: 1

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)

Upvotes: 0

Ryan Bigg
Ryan Bigg

Reputation: 107708

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.

Upvotes: 13

Peter Brown
Peter Brown

Reputation: 51697

It needs to be product_path(product) instead of product in your delete link.

Upvotes: 2

Related Questions