[英]Cannot override $.rails.allowAction
I need to customize confirm boxes in my rails application. 我需要在rails应用程序中自定义确认框。 I found this , added the js code into my application.js but it seems that $.rails is undefined. 我找到了这个 ,将js代码添加到我的application.js中,但似乎$ .rails未定义。
Here is the code I added to application.js file: 这是我添加到application.js文件的代码:
$.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". 我收到错误“Uncaught TypeError:无法设置属性'allowAction'未定义”。
Do you know what's wrong? 你知道什么是错的吗? (jquery_ujs gem is installed and loaded successfully) (jquery_ujs gem已成功安装并加载)
Thank you in advance, 先感谢您,
Jercoh Jercoh
You need: 你需要:
gem 'jquery-rails'
not the jquery_ujs. 不是jquery_ujs。
I put this code into a file called custom.confirm.js and then put those two lines 我将此代码放入名为custom.confirm.js的文件中,然后将这两行放入
//= require jquery_ujs
//= require custom.confirm.js
at the bottom of application.js, and it worked, thanks ! 在application.js的底部,它工作,谢谢!
I just had the same problem, it worked fine on Firefox but not Chrome. 我只是遇到了同样的问题,它在Firefox上工作得很好但不是Chrome。 I guess it was due to Chrome cache and fixed it by cleaning all browser cache related with my development server, 我想这是由于Chrome缓存并通过清理与我的开发服务器相关的所有浏览器缓存来修复它,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.