Skip to content

Instantly share code, notes, and snippets.

@cedricdekimpe
Created May 29, 2012 09:27
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save cedricdekimpe/2823526 to your computer and use it in GitHub Desktop.
Save cedricdekimpe/2823526 to your computer and use it in GitHub Desktop.
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
Copy link

If you are getting a:

Uncaught Error: Invalid argument length 

Change the bootbox.confirm to:

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

@amitpatelx
Copy link

@lobo-tuerto's solution worked for me.

@aditya-kapoor
Copy link

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