How to add confirm message with link_to Ruby on rails

RubyRuby on-Rails-3Ruby on-Rails-3.1Ruby on-Rails-3.2Link To

Ruby Problem Overview


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?

Ruby Solutions


Solution 1 - Ruby

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?'}

Solution 2 - Ruby

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

Solution 3 - Ruby

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?'} %>

Solution 4 - Ruby

<%= link_to 'Reset Message', data: {confirm:"Are you sure?"} %>

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

Solution 5 - Ruby

<%= 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.

Solution 6 - Ruby

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

Solution 7 - Ruby

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...' }) %>

Solution 8 - Ruby

Look at your javascript_include_tag and it should work fine:

<%= link_to("Reset message", :method => :reset, :class => 'action', :confirm => 'Are you sure?') %>

Solution 9 - Ruby

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

Solution 10 - Ruby

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTiniView Question on Stackoverflow
Solution 1 - RubyJamesView Answer on Stackoverflow
Solution 2 - RubydpaluyView Answer on Stackoverflow
Solution 3 - RubyLukasz MuzykaView Answer on Stackoverflow
Solution 4 - RubydiegoView Answer on Stackoverflow
Solution 5 - RubyAnkit WadhwanaView Answer on Stackoverflow
Solution 6 - RubyrubyprinceView Answer on Stackoverflow
Solution 7 - RubyRyosuke HujisawaView Answer on Stackoverflow
Solution 8 - RubySumit MunotView Answer on Stackoverflow
Solution 9 - RubySachin SinghView Answer on Stackoverflow
Solution 10 - RubyV K SinghView Answer on Stackoverflow