Rails 3 how to add a simple confirmation dialog when a user clicks a link

What is the easiest way to ask rails 3 "Are you sure you want to do this?" when does the user click on the link?

the link in this case overwrites some data with the new value. This is a simple link_to (GET) back to the same controller method, with the [] parameter added.

+7
source share
3 answers

EDIT:
<%= link_to "Do something", {:controller => "foo", :action => "bar"}, :confirm => "Are you sure you want to do that?" %>

+9
source

For people like me who came here but are on Rails 4/5:

 link_to "Title", some_path, data: { confirm: "Are you sure?" } 

Documentation

+15
source

There is an option :confirm :

 link_to "Visit Other Site", "http://www.rubyonrails.org/", :confirm => "Are you sure?" # => <a href="http://www.rubyonrails.org/" data-confirm="Are you sure?"">Visit Other Site</a> 

You must have an unobtrusive JavaScript driver, such as this one for jQuery. It is pretty simple to set up.

Details are in the Rails API . You can read more about UJS in Rails 3 in this tutorial .

+5
source

All Articles