Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot override $.rails.allowAction

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

like image 278
Jercoh Avatar asked Nov 13 '12 12:11

Jercoh


2 Answers

You need:

gem 'jquery-rails'

not the jquery_ujs.

like image 198
Dejan Simic Avatar answered Nov 09 '22 23:11

Dejan Simic


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 !

like image 43
Jercoh Avatar answered Nov 09 '22 22:11

Jercoh