Skip to content

Instantly share code, notes, and snippets.

@seyhunak
Created June 28, 2013 09:08
Show Gist options
  • Save seyhunak/5883474 to your computer and use it in GitHub Desktop.
Save seyhunak/5883474 to your computer and use it in GitHub Desktop.
Twitter Bootstrap Confirmation Dialog Modal - Rails UJS
$.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\">&times;</a>\n <h3>Request confirmation</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 btn-huge btn-inverse\">Cancel</a>\n <a data-dismiss=\"modal\" class=\"btn btn-huge btn-danger confirm\">Confirm</a>\n </div>\n</div>";
$(html).modal();
return $('#confirmationDialog .confirm').on('click', function() {
return $.rails.confirmed(link);
});
};
@kikyous
Copy link

kikyous commented Jul 12, 2013

thank you!

@vsrboth
Copy link

vsrboth commented Feb 20, 2014

Hi, i am using this with rails 4, but the $.rails.confirmed(link) is not trigger at all when clicking on a confirm button to delete an active record, any idea why?

@lancefisher
Copy link

@vsrboth, try changing lines 17-19 to this:

  $html = $(html)
  $html.modal();
  var $dialog = $html.find('.confirm');
  return $dialog.on('click', function(a) {

I've also made a fork that uses the Bootstrap 3 modal markup here: https://gist.github.com/lancefisher/11099746

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