How to add confirmation message using link_to Ruby on rails

I wanted to add a confirmation message to the link_to function with Ruby.

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

Any ideas why this is not working?

+54
ruby ruby-on-rails-3 link-to
May 21 '13 at 11:31
source share
10 answers

First you need to make sure your layout has jquery_ujs . This is best done by including it in the main application.js application:

 //= require jquery_ujs 

Make sure you include application.js in your layout:

 = javascript_include_tag :application 

While in development mode, review the HTML source code and check out jquery_ujs.js .

Start the server and make sure that your link tag has data confirmation , for example:

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

If all these steps are correct, everything should work!

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

+43
May 22 '13 at 12:48
source share
β€” -

Maybe I'm wrong, but you will not specify the controller along with the :action option. Have you tried the following? Assuming you have a messages resource configured on your route:

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

EDIT . Above is not recommended. Rails 4.0 now accepts the prompt as a data attribute. See Document here (Thanks @Ricky).

 link_to 'Reset', message_path(@message), :data => {:confirm => 'Are you sure?'} 
+130
May 21 '13 at 13:00
source share

I don’t remember how it 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?'} %> 
+10
May 13 '14 at 2:12
source share

Try the following:

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

or for clearer

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

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

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

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

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

+3
May 21 '13 at 11:38
source share
 <%= 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" %> 

The link to the button where article_path is the prefix, and (article) passes the id that is required for method: :delete method: :delete . The later part of the codes adds a confirmation message.

+2
Nov 13 '18 at 9:26
source share

Look at your javascript_include_tag and it should work fine:

 <%= link_to("Reset message", :method => :reset, :class => 'action', :confirm => 'Are you sure?') %> 
0
May 21 '13 at
source share

Watch videos from railscasts for a better understanding.

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

documentation rails for link_to helper.

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

0
May 21 '13 at 13:51
source share

First, we need to understand which Js package responds to these kinds of alerts in the rails application. Thus, the jquery_ujs package is responsible for displaying warnings in rails.

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

 //= require jquery //= require jquery_ujs 

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

 <%= javascript_include_tag 'application' %> 

Then the link should have data-verify & data-method attributes as

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

In erb it 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 the same way.

0
Jan 10 '18 at 13:35
source share

For some reason, this code does not work only the Safari browser, so I was involved with the button ...

 <%= button_to('', delete_path(), method: "delete", data: { confirm: 'Are you sure?', disable_with: 'loading...' }) %> 
0
Nov 18 '18 at 11:03
source share
 <%= link_to 'Reset Message', data: {confirm:"Are you sure?"} %> 

don't forget to add a path between "discard message" and data

0
Jul 23 '19 at 19:34
source share



All Articles