9

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.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Misha Moroshko
  • 166,356
  • 226
  • 505
  • 746
  • Did you try adding the show action, even though you are not using it so that your routes have you covered?... Just a thought – Richard Dec 15 '10 at 04:50
  • Do you have delete action in your controller? And also try rake:route command to see if delete path is defined properly. – ranendra Dec 15 '10 at 06:06

4 Answers4

13

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.

Ryan Bigg
  • 106,965
  • 23
  • 235
  • 261
  • In the page source I see that `rails.js` is loaded. I do use jQuery 1.4.4. Is this a problem ? I just downloaded it from the official site and put it in `javascript_include_tag`. Should I replace it with files from your link ? What exactly should I download ? – Misha Moroshko Dec 15 '10 at 05:02
  • 2
    @Misha: Download the rails.js file in the src directory and put it into your public/javascripts folder as rails.js. If you're using jQuery, you *must* replace the Prototype `rails.js` with the jQuery equivalent. – Ryan Bigg Dec 15 '10 at 05:40
  • @Ryan: Thanks a lot for guiding me! If I use jQuery rather than Prototype, do I still need `javascript_include_tag :defaults` ? – Misha Moroshko Dec 15 '10 at 09:31
  • I removed `javascript_include_tag :defaults` and put `javascript_include_tag 'jquery-1.4.4.min.js', 'rails.js'` instead (the new `rails.js`, from the link above). The error is still the same: `The action 'show' could not be found for ProductsController`. What else could be wrong ? – Misha Moroshko Dec 15 '10 at 09:49
  • 1
    @Misha: You can change the defaults using this in _config/application.rb_ `config.action_view.javascript_expansions[:default] = ["jquery", "rails"]` – Ryan Bigg Dec 15 '10 at 10:59
  • @Ryan: Nice! So, you say to change the defaults and remove `javascript_include_tag 'jquery-1.4.4.min.js', 'rails.js'`, right ? But, how Rails will know which jQuery file/version to use ? – Misha Moroshko Dec 15 '10 at 12:40
  • The problem was with the `rails.js` file that I downloaded. I downloaded some HTML file instead and called it `rails.js`. So stupid... Now everything works fine. Thanks for your time! – Misha Moroshko Dec 15 '10 at 21:53
5

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

//
//= require jquery
//= require jquery_ujs
// ...
Benjamin Crouzier
  • 40,265
  • 44
  • 171
  • 236
2

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

Peter Brown
  • 50,956
  • 18
  • 113
  • 146
0

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)

John
  • 1