José M. Gilgado
José M. Gilgado

Reputation: 1314

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.

Upvotes: 6

Views: 4175

Answers (1)

dnch
dnch

Reputation: 9605

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 %>

Upvotes: 10

Related Questions