I need to customize confirm boxes in my rails application. I found this, added the js code into my application.js but it seems that $.rails is undefined.
Here is the code I added to application.js file:
$.rails.allowAction = function(link) {
if (!link.attr('data-confirm')) {
return true;
}
$.rails.showConfirmDialog(link);
return false;
};
$.rails.confirmed = function(link) {
link.removeAttr('data-confirm');
return link.trigger('click.rails');
};
$.rails.showConfirmDialog = function(link) {
var html, message;
message = link.attr('data-confirm');
html = "<div class=\"modal\" id=\"confirmationDialog\">\n <div class=\"modal-header\">\n <a class=\"close\" data-dismiss=\"modal\">Ã</a>\n <h3>Are you sure Mr. President?</h3>\n </div>\n <div class=\"modal-body\">\n <p>" + message + "</p>\n </div>\n <div class=\"modal-footer\">\n <a data-dismiss=\"modal\" class=\"btn\">Cancel</a>\n <a data-dismiss=\"modal\" class=\"btn btn-primary confirm\">OK</a>\n </div>\n</div>";
$(html).modal();
return $('#confirmationDialog .confirm').on('click', function() {
return $.rails.confirmed(link);
});
};
I got an error "Uncaught TypeError: Cannot set property 'allowAction' of undefined".
Do you know what's wrong? (jquery_ujs gem is installed and loaded successfully)
Thank you in advance,
Jercoh
You need:
gem 'jquery-rails'
not the jquery_ujs.
I put this code into a file called custom.confirm.js and then put those two lines
//= require jquery_ujs
//= require custom.confirm.js
at the bottom of application.js, and it worked, thanks !
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