Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

link_to with delete and own controller and action

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

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

The problem shows up 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 %>

It doesn't work, the url is just like a get, and the anchor hasn't got the data-remote and the others attributes from Rails.

So, how could I use my own controller and action with link_to and the delete method?

I have the route in the routes.rb file so I think that isn't the problem.

Thanks in advance.

like image 992
José M. Gilgado Avatar asked May 03 '11 11:05

José M. Gilgado


1 Answers

When you provide your URL as a hash of options, you need to be a little clearer about which hash is which. Try this:

<%= 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 %>
like image 88
dnch Avatar answered Oct 23 '22 05:10

dnch