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?
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?'}} %>
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
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?" })
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With