rails 3, how add a simple confirmation dialog when user clicks a link
Asked Answered
H

4

11

what is the easiest way in rails 3 to to ask "Are you sure you want to do that?" when the user clicks on a link?

the link in this case overwrites some data with a new value. It's a simple link_to (GET) back to the same controller method, with a param[] added.

Hoitytoity answered 3/4, 2011 at 9:9 Comment(0)
H
11

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

Heurlin answered 3/4, 2011 at 9:21 Comment(0)
T
24

For those like me that came here but are on Rails 4/5:

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

The documentation

Talaria answered 29/3, 2016 at 14:36 Comment(0)
H
11

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

Heurlin answered 3/4, 2011 at 9:21 Comment(0)
T
5

There is a :confirm option:

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 should have unobtrusive JavaScript driver like this one for JQuery. It's pretty simple to setup.

Details are in Rails API. For more details about UJS in Rails 3 you can follow this tutorial.

Translator answered 3/4, 2011 at 9:28 Comment(0)
A
1

If you use rails version 7 you can do it in this way:link_to "Title", some_path, form: { data: { turbo_confirm: "Are you sure?" } }

Alejoa answered 5/8, 2022 at 18:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.