I wanted to add confirmation message on link_to function with Ruby.
= link_to \'Reset message\', :action=>\'reset\' ,:confirm=>\'Are you sure?\'
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
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?'}
<%= 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.
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?'} %>
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
Look at your javascript_include_tag and it should work fine:
<%= link_to("Reset message", :method => :reset, :class => 'action', :confirm => 'Are you sure?') %>