1

I am stuck at Delete/Destroy. Any help would be appreciated!

I am using Ruby 2.0.0 and Rails 3.2.6 on Mac 10.8.3 with Postgres.

This is the delete link which supposed to work:

<%= link_to 'Destroy', @product, method: :delete, data: { confirm: 'Are you sure?' } %>

But when I click on the Destroy link it directs me to the user's profile. I don't get any confirmation window and no action is done (delete). It seems it is just stayed at the same page. I am using Firefox but in IE and Chrome are the same.

This is what I have:

  1. the gem "jquery-rails" is installed

  2. applications.js (from app/assets/javascripts) has these lines:

    //= require jquery
    //= require jquery_ujs
    //= require_tree .
    
  3. application.html.erb (from app/views/layouts) has these lines:

    <%= javascript_include_tag "application" %>
    <%= csrf_meta_tags %>
    

    In view source I see:

    <script src="/assets/application.js?body=1" type="text/javascript"></script>
    <meta content="authenticity_token" name="csrf-param" />
    <meta content="MVlJi+WJE1cwWoHnBrpRWIa13gqio0iPT3IL6kpQYdE=" name="csrf-token" />
    
  4. in products_controllers.rb (from app/controllers) I have:

    def destroy
        @product = Product.where(:id => params[:id]).first
        @product.destroy
    
        respond_to do |format|
            format.html { redirect_to products_url }
            format.json { head :no_content}
        end
    end
    
  5. in routes.rb (from config) I have:

    resources :products
    

What is wrong with that Destroy link?

On the same RoR project the Edit link works fine.

So, why the link doesn't work? Is it a Javascript problem or some other problem which I am not seeing?

And here is the server's log: (It seems the "delete" action does not been executed)

Started GET "/products/9" for 127.0.0.1 at 2013-08-01 20:01:08 -0500
Processing by ProductsController#show as HTML
Parameters: {"id"=>"9"}
Product Load (0.1ms)  SELECT "products".* FROM "products" WHERE "products"."id" = 9 LIMIT 1
Rendered products/show.html.erb within layouts/application (0.5ms)
Completed 200 OK in 27ms (Views: 25.8ms | ActiveRecord: 0.1ms)
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Lisa
  • 2,809
  • 3
  • 24
  • 37
  • Search in browser console for `JavaScript` errors. – Marek Lipka Aug 01 '13 at 14:42
  • Try with `link_to 'Destroy', product_path(@product.id), method: :delete` and also take a look at this: http://stackoverflow.com/questions/4423314/link-to-delete-url-is-not-working – MrYoshiji Aug 01 '13 at 14:49
  • See your server's log. – cortex Aug 01 '13 at 14:53
  • Please provide some information about the error from the server log. – MarkoHiel Aug 01 '13 at 15:00
  • @MrYoshiji, hey, I tried this, but it does not work for me....:( – Lisa Aug 02 '13 at 00:59
  • @cortex, hey, according to the log, it seems the "delete" does not executed. – Lisa Aug 02 '13 at 01:01
  • Here is the server's log: Started GET "/products/9" for 127.0.0.1 at 2013-08-01 20:01:08 -0500 Processing by ProductsController#show as HTML Parameters: {"id"=>"9"} Product Load (0.1ms) SELECT "products".* FROM "products" WHERE "products"."id" = 9 LIMIT 1 Rendered products/show.html.erb within layouts/application (0.5ms) Completed 200 OK in 27ms (Views: 25.8ms | ActiveRecord: 0.1ms) – Lisa Aug 02 '13 at 01:01
  • @MarkoHiel there is no error message here...I added the server log here...Thanks! – Lisa Aug 02 '13 at 01:04
  • In order to debug, you could add a logging information like `pp "Destroy"`in your controller destroy action. In order to see if your request is passing that part of your app. – MarkoHiel Aug 02 '13 at 08:50

2 Answers2

0

One helpful way to handle path issues with Rails is to type "rake routes" in your console, in order to have a list of all the paths available to play with your ressources.

By declaring "resources :products" in your routes.rb file, "rake routes" will provide a list of Prefix/Verb/URI Pattern/Controller#Action for this particular resource. You will have something like:

Prefix  Verb   URI Pattern
product GET    /product/:id(.:format) product#show
        PATCH  /product/:id(.:format) product#update
        DELETE /product/:id(.:format) product#destroy

Take the word of the prefix column (product), add _path to this word (product_path), use the correct method (DELETE), pass the ids as parameters (in this case, you can just do product_path(@product)), and you're done. You will have :

<%= link_to "Delete", product_path(@product), method: :delete, data: { confirm: "Are you sure?" } %>
d34n5
  • 1,308
  • 10
  • 18
  • Hey, I tried rake routes, and I did get what you typed above, I changed my code into what you typed here...It did not work.....-_-|| – Lisa Aug 02 '13 at 01:10
  • Do I have to put the "<%= link_to "Delete", product_path(@product), method: :delete, data: { confirm: "Are you sure?" } %>" into a separate view? I put it in the show.html.erb right now... – Lisa Aug 02 '13 at 01:11
0

Perhaps (I'm just guessing, but one day I ran into a similar problem) if you design catalog or shop with a shopping cart based on the book Agile Web Development with Rails, the product model may contain a filter before_destroy :ensure_not_referenced_by_any_line_item that prevents the product from destroy in case if product listed in LineItems. If this is your case, you can clear list of LineItems in the ProductsController before destroying the product. Otherwise sorry.