javascript - Cannot override $.rails.allowAction - Stack Overflow

I need to customize confirm boxes in my rails application. I found this, added the js code into my appl

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

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

Share Improve this question edited Nov 13, 2012 at 13:42 Jercoh asked Nov 13, 2012 at 12:16 JercohJercoh 1032 silver badges7 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

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 !

I just had the same problem, it worked fine on Firefox but not Chrome. I guess it was due to Chrome cache and fixed it by cleaning all browser cache related with my development server,

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745356403a4624144.html

相关推荐

  • javascript - Cannot override $.rails.allowAction - Stack Overflow

    I need to customize confirm boxes in my rails application. I found this, added the js code into my appl

    5小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信