Destroy a path not working in Ruby on Rails

Completely dead end on this subject, as I believe it is using the correct setting, but I cannot 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 

What gives the correct exit route routes:

  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 still get the following error when working in a 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 might cause this, any help would be greatly appreciated :)

+4
source share
4 answers

Updated answer.

Your link_to ad looks great:

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

You probably don't have the necessary javascript included in your layout file:

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

therefore, list_path cannot execute the destroy action and tries to execute the show action.

If this does not work, a problem elsewhere prevents javascript from executing.

+15
source

After looking at the log output, the link sends a GET request instead of a DELETE request. In rails, the connection with method: :delete changes from a GET request to a DELETE request through JavaScript.

My guess is that you are not including default javascript files, javascript defaults in your application.js application or through = javascript_include_tag :defaults , which will give you jquery and jquery.ujs for Rails.

Is it possible to verify that jquery and jquery.ujs are uploaded to your page using the delete link?

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

In addition, I suggest that you follow this Railscasts in unobtrusive Javascript for more information.

+6
source

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

+2
source

In your controller, change @ list.destroy in the destroy method to @ list.delete. This has worked for me in the past. Hope this helps.

+1
source

Source: https://habr.com/ru/post/1501505/


All Articles