Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add confirm message with link_to Ruby on rails

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?

like image 951
Tini Avatar asked May 21 '13 11:05

Tini


2 Answers

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?'}
like image 142
James Avatar answered Oct 10 '22 15:10

James


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

like image 43
dpaluy Avatar answered Oct 10 '22 13:10

dpaluy