Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
How-to override window.confirm() dialog with Rails 3 and bootbox.js
//= require bootbox.min
$(document).ready(function() {
$.rails.allowAction = function(element) {
var message = element.data('confirm'),
answer = false, callback;
if (!message) { return true; }
if ($.rails.fire(element, 'confirm')) {
myCustomConfirmBox(message, function() {
callback = $.rails.fire(element,
'confirm:complete', [answer]);
if(callback) {
var oldAllowAction = $.rails.allowAction;
$.rails.allowAction = function() { return true; };
element.trigger('click');
$.rails.allowAction = oldAllowAction;
}
});
}
return false;
}
function myCustomConfirmBox(message, callback) {
bootbox.confirm(message, "Cancel", "Yes", function(confirmed) {
if(confirmed){
callback();
}
});
}
});
@lobo-tuerto

This comment has been minimized.

Copy link

@lobo-tuerto lobo-tuerto commented Nov 21, 2013

If you are getting a:

Uncaught Error: Invalid argument length 

Change the bootbox.confirm to:

bootbox.confirm(message, function(confirmed) {
    if(confirmed){
        callback();
    }
});
@amitpatelx

This comment has been minimized.

Copy link

@amitpatelx amitpatelx commented Dec 27, 2013

@lobo-tuerto's solution worked for me.

@aditya-kapoor

This comment has been minimized.

Copy link

@aditya-kapoor aditya-kapoor commented Jun 24, 2015

Not working in case of

<%= link_to 'some-link.com', class: 'some-class', data: { confirm: 'Are you sure?' } do %>
 <span>hi...</span>
<% end %>

In short when the method is 'GET', it is not working. Can you pls confirm.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment