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.
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 %>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With