Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why confirm option of rails button_to helper is not working

I have a rail button_to where I want it to show a confirm dialog box before proceeding.

<%= button_to("Rebuild indexes", action: "rebuild_indexes", data:
{confirm: "Are you sure you want to reset the indexes?" }) %>

The actions are occurring but I'm not getting a confirmation box. I read that the problem could be related to javascript. I thought I had that enabled.

application.js
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require jquery
//= require jquery_ujs
//= require jquery.turbolinks
//= require turbolinks
//= require tinymce-jquery
//= require ckeditor/init
//= require ckeditor/config.js
//= require jquery.are-you-sure
//= require script
//= require script.responsive
//= require_tree .

If I do an inspect on the button, I see

<form class="button_to" method="post" action="/help/rebuild_indexes?data%5Bconfirm%5D=Are+you+sure+you+want+to+reset+the+indexes%3F">
  <input type="submit" value="Rebuild indexes">
  <input type="hidden" name="authenticity_token" value="3mnAMmyHaINiDngjQn87EMmhmetp2VJcX+lwcmbUlhwBfv7V1hpLc9ZY1OF5JAFm9erFJjX+qyDz35/KK41jaA==">
</form>

What am I missing in getting a confirmation box to appear?

like image 502
Chris Mendla Avatar asked Jan 27 '16 16:01

Chris Mendla


3 Answers

Just a quick note for those who want to create a button_to with confirmation text (in rails 7):

<%= button_to 'Rebuild indexes', action: :rebuild_indexes,
  form: {data: {turbo_confirm: 'Are you sure?'}} %>
like image 91
installero Avatar answered Oct 29 '22 19:10

installero


I was having a similar issue with the following line:

 <%= button_tag 'Delete', onclick: "location.href = #{polymorphic_url(obj)}", :method => :delete, type: 'button', class: 'btn btn-danger', title: 'Delete', id: 'delete_btn', data: { confirm: 'Are you sure you want to delete this record?' } %>

Removing "type: 'button'" fixed the confirm prompt issue

like image 44
Stephen Avatar answered Oct 29 '22 20:10

Stephen


data: {confirm: "<confirm message>"} is being tacked on to the url parameters hash instead of being passed as a third parameter - try separating the hashes like this:

button_to("Rebuild indexes", {action: "rebuild_indexes"}, data: {confirm: "Are you sure you want to reset the indexes?" })
like image 32
Fred Willmore Avatar answered Oct 29 '22 21:10

Fred Willmore