Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Destroy path not working in Ruby on Rails

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 :)

like image 734
Tom Pinchen Avatar asked Sep 10 '13 15:09

Tom Pinchen


2 Answers

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.

like image 196
vee Avatar answered Nov 06 '22 20:11

vee


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.

like image 36
Carlos Drew Avatar answered Nov 06 '22 21:11

Carlos Drew