5

Totally stumped on this as I believe it's using the correct restful setup but I can't seem to delete my list.

I have the following:

Controller:

def destroy
    @list = List.find(params[:id])
    @list.destroy
    redirect_to lists_path
end 

#37 def show
#38     @list = List.find(params[:id])
#39 end

Index.html.haml

- @list.each do |list| 
    ...
    = link_to "Delete", list_path(list), method: :delete, data: { confirm: 'Are you certain you want to delete this?' }
    ...

Routes:

ListApp::Application.routes.draw do
    devise_for :users
    resources :lists

Which is giving the correct rake routes output of:

           lists GET    /lists(.:format)               lists#index                                                                                                                                                                     
                 POST   /lists(.:format)               lists#create                                                                                                                                                                    
        new_list GET    /lists/new(.:format)           lists#new                                                                                                                                                                       
       edit_list GET    /lists/:id/edit(.:format)      lists#edit                                                                                                                                                                      
            list GET    /lists/:id(.:format)           lists#show                                                                                                                                                                      
                 PATCH  /lists/:id(.:format)           lists#update                                                                                                                                                                    
                 PUT    /lists/:id(.:format)           lists#update                                                                                                                                                                    
                 DELETE /lists/:id(.:format)           lists#destroy

But I am still getting the following error when running in production environment.

I, [2013-09-10T15:27:40.338022 #453]  INFO -- : Started GET "/lists/2" for 81.159.35.212 at 2013-09-10 15:27:40 +0000                                                                                                                          
I, [2013-09-10T15:27:40.340626 #453]  INFO -- : Processing by ListsController#show as HTML                                                                                                                                                     
I, [2013-09-10T15:27:40.340867 #453]  INFO -- :   Parameters: {"id"=>"2"}                                                                                                                                                                      
I, [2013-09-10T15:27:40.354092 #453]  INFO -- : Completed 500 Internal Server Error in 13ms                                                                                                                                                    
F, [2013-09-10T15:27:40.359807 #453] FATAL -- :                                                                                                                                                                                                
NameError (undefined local variable or method `list' for #<ListsController:0x000000050a7c38>):                                                                                                                                                 
  app/controllers/lists_controller.rb:38:in `show'  

If anyone knows what could be causing this any help would be much appreciated :)

Tom Pinchen
  • 2,467
  • 7
  • 33
  • 53
  • 2
    the error is in the `show` action as per the error text - app/controllers/lists_controller.rb:38:in `show' - can you show that code? – dax Sep 10 '13 at 15:37
  • Hey Dax, Thanks for you reply. I don't actually have a show page for any lists, just index. It is from the index page that this delete method is meant to be being called. Any other ideas? :) – Tom Pinchen Sep 10 '13 at 15:47
  • 1
    Could you show us code with line #38 on the ListsController ie the line on which the error occurs? – Carlos Drew Sep 10 '13 at 17:31
  • I have added line 38 to the controller above. It's erroring trying to load the show page when really it should be calling delete. – Tom Pinchen Sep 11 '13 at 08:49

5 Answers5

18

Updated answer.

Your link_to declaration looks fine:

= link_to "Delete", list_path(list), method: :delete, data: { confirm: 'Are you certain you want to delete this?' }

It' likely that you do not have necessary javascript included in your layout file:

# app/views/layouts/application.html.haml
%head
  = javascript_include_tag "application"
  = csrf_meta_tag %>

which is why the list_path is not able to execute the destroy action and is trying to execute the show action.

If this does not work, then the problem is elsewhere that's preventing the javascript from being executed.

vee
  • 38,255
  • 7
  • 74
  • 78
  • 1
    i think you mean that the other way around - `list_path` not `lists_path` – dax Sep 10 '13 at 15:40
  • Hi Vinodadhikary, thanks for your reply. Lists path is just loading the URL `http://third-box-27032.euw1.actionbox.io:3000/lists.2` but not calling the delete method or giving the data confirm. List_path was working fine in development, it's only in production where it's failing? Any other ideas? – Tom Pinchen Sep 10 '13 at 15:45
  • @TomPinchen, can you try: `= link_to "Delete", list_path(list), method: :delete, remote: true, data: { confirm: 'Are you certain you want to delete this?' }` – vee Sep 10 '13 at 16:01
  • Sadly this doesn't work either, I am getting an error `ActionView::MissingTemplate (Missing template lists/show, application/show` which suggests to me it's not finding the delete method at all :( – Tom Pinchen Sep 10 '13 at 16:10
  • Thanks for your reply. I have the correct javascript_include_tag and csrf_meta_tag included bit the addition of `"data-turbolinks-track" => true` I wonder if perhaps this is causing the issue. My precompiled assets also don't seem to be being found despite being in public/assets and having a working manifest MD5 file. I will keep working on it. Thanks again for your help! – Tom Pinchen Sep 10 '13 at 17:02
  • I had the same issue. I was using `layout 'admin'` in the controller to target a specific `admin.html.erb` in layouts folder instead of the `application.html.erb`. When I deleted it the destroy method got back to work. Might be a rails bug? – a.barbieri May 25 '16 at 12:55
  • UPDATE: Apparently in the `head` of my view I was missing `<%= csrf_meta_tags %>`. That was confusing my destroy method. – a.barbieri May 25 '16 at 13:02
6

Looking at your log output, the link is sending a GET request instead of a DELETE request. In rails, a link with method: :delete gets changed from a GET request to a DELETE request via JavaScript.

My hunch is that you are not including the Rails default jquery javascript files either in your application.js or via = javascript_include_tag :defaults, which will get you jquery and jquery.ujs for Rails.

Can you verify that jquery and jquery.ujs are loaded on your page with the delete link?

Alternately, try adding = javascript_include_tag :defaults to your view/layout and see if the link starts working.

Also, I suggest that you watch and follow this Railscasts on Unobtrusive Javascript for more information.

Carlos Drew
  • 1,633
  • 9
  • 17
  • Thanks for your reply. I think you're right and it's not loading the Javascript correctly but this seems strange as everything is setup as normal in the application.html.erb and do it should work. Even adding the Javascript defaults tag to the actual index page isn't working. :S – Tom Pinchen Sep 11 '13 at 08:46
  • Are you using a web inspector in your browser? Do you see any JS errors when you click on the link? Try turning on console history in the web inspector so you can see any errors after clicking through the link: https://developers.google.com/chrome-developer-tools/docs/tips-and-tricks#console-persist – Carlos Drew Sep 11 '13 at 16:45
  • Here's a test for you: jump into your JS console on your production page with the delete link. Enter this command `$.rails == undefined`. If the console spits back `true`, then you don't have rails/jquery_ujs loaded into your assets. `$.rails` is the jquery_ujs handler for Rails requests. So, if production doesn't have jquery_ujs, you have an assets pipeline problem. Could you post your full %head and application.js? – Carlos Drew Sep 11 '13 at 16:58
2

Try linking to lists_path instead of list_path. Look at your routes.

NM Pennypacker
  • 6,704
  • 11
  • 36
  • 38
2

In your controller change the @list.destroy in the 'destroy method' to @list.delete . It worked for me in the past. Hope it helps.

  • 1
    Not a good practice though. If you have other models associated with @list, this will throw an error as delete tries to directly delete the concerned record. The destroy will call destroy on it's associations (has_many) too if you add `dependent: :destroy' in front of the has_many association in the list model, their destroy method will be called before your concerned destroy i.e. a cascading effect. – ARK Dec 18 '19 at 15:05
1

I know the question is old , but if some beginner is struggling with delete button not working. try instead of link_to to use button_to it fixed it for me

myanch200
  • 76
  • 2
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 10 '22 at 04:00