76

I wanted to add confirmation message on link_to function with Ruby.

= link_to 'Reset message', :action=>'reset' ,:confirm=>'Are you sure?'

Any ideas why it's not working?

Anand Shah
  • 14,575
  • 16
  • 72
  • 110
Tini
  • 769
  • 1
  • 6
  • 7
  • Possible duplicate of [Rails - How to put a confirmation popup on a f.submit?](http://stackoverflow.com/questions/15771582/rails-how-to-put-a-confirmation-popup-on-a-f-submit) – mmike Sep 01 '16 at 08:37
  • This does not appear to be an exact duplicate of the nominated exemplar. The exemplar asks how to add confirmation to a form submission; this question asks how to add confirmation to a link. – Wayne Conrad Sep 01 '16 at 08:42

12 Answers12

151

I might be mistaken but you don't specify a controller along with the :action option. Have you tried the following? Assuming you have a messages resource configured in your route:

link_to 'Reset', message_path(@message), :confirm => 'Are you sure?'

EDIT: Above is deprecated. Rails 4.0 now accepts the prompt as a data attribute. See the doc here (Thanks @Ricky).

link_to 'Reset', message_path(@message), :data => {:confirm => 'Are you sure?'}
James
  • 4,599
  • 2
  • 19
  • 27
  • 10
    I think this style is deprecated. According to the rails docs, this is now a data attribute, i.e. `…, data: {confirm: 'Are you sure?'}` –  Sep 17 '14 at 16:08
  • @Ricky you do realize this question and almost all the answers on this page were posted on May 21, 2013? It is Sept 2014 right now. – James Sep 17 '14 at 19:44
  • 3
    Yeah. Forgive me—I'm not fully immersed in stackoverflow culture! Someone asked about this, got directed here and was confused about why `:confirm` didn't seem to work. `'data-confirm'` works but it's not really the current 'rails way'. In other words: what's the best way for me to make a note of this? –  Sep 19 '14 at 06:05
  • 3
    @Ricky No I should've just updated my answer to begin with so I'm sorry. I must've been having a bad day or something. Answer edited with Rails 4.0 api. – James Sep 19 '14 at 11:03
  • [Testing JS popups can help prevent slipping through the cracks.](http://stackoverflow.com/questions/6930927/how-do-i-confirm-a-javascript-popup-with-capybara) – brntsllvn Nov 10 '15 at 18:09
  • I'm working on an older app we have at work and it's on Rails 3.2.8. This helped me understand why my first (newer) solution wouldn't actually give me the confirmation dialog. Thanks! – keaglin Feb 19 '20 at 22:44
  • Typo. We're on Rails 3.2.18. – keaglin Feb 19 '20 at 22:56
52

First, you should verify that your layout have jquery_ujs. Best practice to do it by including it in your main application.js:

//= require jquery_ujs

Check that you included application.js in your layout:

= javascript_include_tag :application

While, in development mode, view your source html and verify jquery_ujs.js exists.

Run your server and verify your link tag has data-confirm value, for example:

<a href="/articles/1" data-confirm="Are you sure?" data-method="delete">

If all those steps are correct, everything should work!

Note: check this RailsCast http://railscasts.com/episodes/136-jquery-ajax-revised

dpaluy
  • 3,537
  • 1
  • 28
  • 42
13

Can't remember how this was done in Rails 3, but in Rails 4 you can simply:

<%= link_to 'Reset message', { controller: 'your_controller', action: 'reset' }, data: {confirm: 'Are you sure?'} %>
Lukasz Muzyka
  • 2,783
  • 1
  • 30
  • 41
  • 1
    This answer helped me the most; grouping the parameters separately with `{` and `}` before the `data - confirm` part was what I had to do. – Prakash Murthy Jun 29 '16 at 22:17
13
<%= link_to 'Reset Message', data: {confirm:"Are you sure?"} %>

remember to add the path, between 'reset message' and data

diego
  • 409
  • 4
  • 12
4
<%= link_to "Delete this article", article_path(article), method: :delete,
                    data: { confirm: "Are you sure you want to delete the 
                    article?"}, class: "btn btn-xs btn-danger" %>

A button link where article_path is the prefix and (article) is passing the id which is required by the method: :delete method. The later part of the codes adds a confirmation msg.

Ankit Wadhwana
  • 325
  • 3
  • 9
3

Try this:

= link_to 'Reset message', {:action=>'reset'}, :confirm=>'Are you sure?'

or to be more clear

= link_to('Reset message', {:action=>'reset'}, {:confirm=>'Are you sure?'})

Refer http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to

You will see that there are 3 parameters, when you are giving url as options like {:action => ..., :controller => ...}

link_to(body, url_options = {}, html_options = {})

In ruby, if the last parameter in a function call is a hash, you need not wrap it in {} characters (in other words, you can omit that in case, if the hash is the last parameter), so the code you have provided will be interpreted as a function call with only 2 parameters, 'Reset message' string and {:action=>'reset', :confirm=>'Are you sure?'} hash and the :confirm=>'Are you sure?' will be interpreted as a url_option instead of a html_option

rubyprince
  • 17,559
  • 11
  • 64
  • 104
  • This is new code as you suggested = link_to('Reset message', {:action=>"reset"} ,{:confirm=>"Are you sure?"}).... and this is html Reset message What is 'data-confirm'....stil not opened confirm message...Thanks. – Tini May 21 '13 at 12:15
  • `data-*` are [custom data attributes](http://www.w3.org/html/wg/drafts/html/master/dom.html#embedding-custom-non-visible-data-with-the-data-*-attributes) introduced in HTML5 which serve as storage areas for private data. `data-confirm` is used by rails to make the pop up appear when the link is clicked. – James May 21 '13 at 12:49
  • @Tini...have you included `<%= javascript_include_tag :defaults %>` in the layout or page. Without that it wont work. – rubyprince May 22 '13 at 04:47
1

Somehow does not work those code only Safari browser So I was involved button...

<%= button_to('', delete_path(), method: "delete", data: { confirm: 'Are you sure?', disable_with: 'loading...' }) %>
Ryosuke Hujisawa
  • 2,682
  • 1
  • 17
  • 18
1

In rails 7 with Turbo, I tried the solutions above and they didn't work for me, but this worked for me.

Tip: copy the 4 methods below directly into a page of your app and test them!

<%= link_to "GET turbo link", "/",
  data: {
    turbo_method: :get,
    turbo_confirm: "Sure?"
  }
%><br>

<%= link_to "POST turbo link", "/",
  data: {
    turbo_method: :post,
    turbo_confirm: "Sure?"
  }
%>

<%= button_to "Button", "/",
  data: { turbo_confirm: "Sure?" }
%>

<%= form_with url: "/",
  data: { turbo_confirm: "Sure?" } do |f| %>

  <%= f.submit "Submit" %>
<% end %>

This info comes from this incredibly helpful answer: https://stackoverflow.com/a/75046023/5783745

stevec
  • 41,291
  • 27
  • 223
  • 311
0

Look at your javascript_include_tag and it should work fine:

<%= link_to("Reset message", :method => :reset, :class => 'action', :confirm => 'Are you sure?') %>
Sumit Munot
  • 3,748
  • 1
  • 32
  • 51
  • As pointed above, `:confirm` is now deprecated so you have to use either `"data-confirm" => ...` or `data: { confirm: ... }`. – lucasarruda Dec 22 '15 at 18:20
0

watch this railscasts video for better understanding.

http://railscasts.com/episodes/205-unobtrusive-javascript

rails documentation for link_to helper.

http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to

Sachin Singh
  • 7,107
  • 6
  • 40
  • 80
0

First, we need to understand what Js package respond to this kind of alerts in rails application. So for this, jquery_ujs package is reponsible for showing the alerts in rails.

So you must have jquery & jquery_ujs in your application.js file.

//= require jquery
//= require jquery_ujs

Now, we need to confirm, that application.js file is included in your required layout or not. By default layout file remains in application.html.erb in layout folder of views.

<%= javascript_include_tag 'application' %>

Next the link should have data-confirm & data-method attributes as

<a href="/message/1/reset" data-method="delete" data-confirm="Are you sure?">

In erb, this can be written as,

= link_to 'Reset', message_path(@message), data: {method: 'delete', confirm: 'Are you sure?'}

This should work if everything is aligned in same fashion.

V K Singh
  • 1,134
  • 9
  • 14
0

The UJS library, which drove the confirm functionality you're describing, is no longer enabled by default in Rails 7. That functionality has been superseded by Turbo, but there's an upgrade guide to allow them to play nicely.

Instead, I would recommend implementing a modal-based confirmation, which Turbo makes incredibly easy. GoRails has an easy tutorial on how to implement it.

One gotcha I missed: make sure to use button_to (not link_to), and don't nest the button within another form.

Frank Koehl
  • 3,104
  • 2
  • 29
  • 37