Tag Archives: api

Using link_to in rails with :confirm and :method

Every time i’m going to use the link_to method in rails, I always seem to get a lot of errors.

The documentation states:

link_to(name, options = {}, html_options = nil)
link_to(options = {}, html_options = nil) do
# name

URL to the rails docs: http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#M001597

And gives a list of options you can set. However, I always seem to have problems remembering the correct syntax if I want to use :confirm and :method.

After fiddling with it a bit, I found that the proper way of using is:

%lt%= link_to(
    :action => "destroy",
    :id => some_id
  :method => :delete,
  :confirm => t('Are you sure you want to delete it?')
) %>

My biggest problem was, that you do not need to add the controller.

Also, found a nice write up about this on Ryan’s Scraps blog (a bit old post, but still…)

Hope you can use the tip.