'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?
Solution 1:[1]
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?'}} %>
Solution 2:[2]
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
Solution 3:[3]
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?" })
Solution 4:[4]
Try adding link_to instead of button_to. '/indexes/rebuild' is the route.
<%= link_to "Rebuild indexes", '/indexes/rebuild', data: { confirm: 'Are you sure you want to reset the indexes?' }, class:"btn btn-default" %>
Solution 5:[5]
<%= button_to "/home/delete?cardId="+card.id.to_s, data: { confirm:'Are you sure you want to delete?' } do %>
<i class="fa fa-times"></i>
<% end%>
this generate
<form class="button_to" method="post" action="/home/delete?cardId=15">
<button data-confirm="Are you sure you want to delete?" type="submit">
<i class="fa fa-times"></i>
</button>
</form>
Solution 6:[6]
This is known issue with turbolink feature. You would need to explicitly turn off turbolink for the helper method, by insert 'turbolinks: false' into data attribute:
<%= button_to("Rebuild indexes", action: "rebuild_indexes", data:
{turbolinks: false, confirm: "Are you sure you want to reset the indexes?" }) %>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Stephen |
| Solution 3 | Fred Willmore |
| Solution 4 | petrified |
| Solution 5 | Matias Sanchez |
| Solution 6 | sam |
