Link_to with deletion and own controller and action

I have the following problem in Ruby on Rails 3. When I try to use the link_to method in a view with the parameter: method =>: delete and the object, as usual, it works fine.

<%= link_to 'Delete', @car , :confirm => 'Are you sure?', :method => :delete, :remote => true %> 

The problem occurs when I try to use my own controller and action:

 <%= link_to 'Delete', :id => @car.id, :confirm => 'Are you sure?', :controller => 'truck', :action => 'my_destroy', :method => :delete, :remote => true %> 

This does not work, the URL is similar to get, and the binding has no data-removed and other attributes from Rails.

So, how can I use my own controller and action using link_to and the delete method?

I have a route in the routes.rb file, so I think this is not a problem.

Thanks in advance.

+6
source share
1 answer

When you specify your URL as an options hash, you need to be a little clearer about what the hash is. Try the following:

 <%= link_to 'Delete', { :controller => 'truck', :action => 'my_destroy', :id => @car.id }, # your URL details { :confirm => 'Are you sure?', :method => :delete, :remote => true} # your link options %> 
+10
source

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


All Articles