Rails 中的 rjs 类型错误?
我添加了一个 link_to_remote
来从局部加载模式对话框。但是当我运行该应用程序时,出现以下错误。
RJS error:
TypeError: $("create_event_dialog").dialog is not a function
当我单击“确定”时,我再次出现另一个对话框。
Element.update("create_event", "<form action=\"/countries\" class=\"new_country\" id=\"new_country\" method=\"post\" onsubmit=\"new Ajax.Request('/countries', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;\"><div style=\"margin:0;padding:0;display:inline\"><input name=\"authenticity_token\" type=\"hidden\" value=\"qWXCu2zMmlMhJG+GRf35kMbAIfzYAtFBee142ThmmMw=\" /></div>\n <p>\n <label for=\"country_name\">Name</label><br />\n <input id=\"country_name\" name=\"country[name]\" size=\"30\" type=\"text\" />\n </p>\n <p>\n <input id=\"country_submit\" name=\"commit\" type=\"submit\" value=\"Create\" />\n </p>\n</form>\n");
$('create_event_dialog').dialog({
title: 'New Event',
modal: true,
width: 500,
close: function(event, ui) { $('create_event_dialog').dialog('destroy') }
});
为什么我会出现这种错误?我有一个 ID 为 create_event_dialog
的 div。我正在使用 Prototype,但找不到解决此问题的方法。请帮我。
I add a link_to_remote
to load a modal dialog box from a partial. But when I run the application I get the following error..
RJS error:
TypeError: $("create_event_dialog").dialog is not a function
When I click ok then again I got another dialog box.
Element.update("create_event", "<form action=\"/countries\" class=\"new_country\" id=\"new_country\" method=\"post\" onsubmit=\"new Ajax.Request('/countries', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;\"><div style=\"margin:0;padding:0;display:inline\"><input name=\"authenticity_token\" type=\"hidden\" value=\"qWXCu2zMmlMhJG+GRf35kMbAIfzYAtFBee142ThmmMw=\" /></div>\n <p>\n <label for=\"country_name\">Name</label><br />\n <input id=\"country_name\" name=\"country[name]\" size=\"30\" type=\"text\" />\n </p>\n <p>\n <input id=\"country_submit\" name=\"commit\" type=\"submit\" value=\"Create\" />\n </p>\n</form>\n");
$('create_event_dialog').dialog({
title: 'New Event',
modal: true,
width: 500,
close: function(event, ui) { $('create_event_dialog').dialog('destroy') }
});
Why do I got this kind of error? I have a div with the id of create_event_dialog
. I am using Prototype and I could not find the way to solve this. Please help me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的代码看起来像您想要实际使用 jQuery UI 对话框 功能。据我所知,Prototype(或 Scriptaculous)没有内置对话框支持。
如果想要一个 jQuery UI 对话框,您有两个选择:
选项 1:
要与 Prototype 一起使用 jQuery,您必须包含 jQuery Javascript 库并通过调用
jQuery.noConflict()
启用兼容模式。之后$
函数属于 Prototype,对于所有 jQuery 功能,您必须使用jQuery(...)
代替。例如,在您的情况下jQuery('create_event_dialog').dialog(...
。您可以在 将 jQuery 与其他库一起使用
选项 2:
您必须删除 Prototype 库并包含 jQuery 库。您还需要 jRails 兼容性库 来保留原型助手,并且您必须将现有的原型代码更改为 jQuery 代码
。
Element.update
语句如下所示:参见将 jQuery 与 Ruby on Rails 结合使用 了解更多详细信息:
小心: 根据您现有的代码,选项 2 可能需要大量工作,但会产生更干净的 Javascript 代码。此外,可能值得考虑切换到 Rails 3.1。 UJS 和 jQuery 作为官方 Javascript 库。
Your code looks like you want to actually use the jQuery UI dialog feature. As far as I know Prototype (or Scriptaculous) has no built-in dialog support.
If want to have a jQuery UI dialog you have two options:
Option 1:
To use jQuery alongside Prototype you have to include the jQuery Javascript libraries and enable the compatibility mode by calling
jQuery.noConflict()
. After that the$
function belongs to Prototype and for all jQuery features you have to usejQuery(...)
instead. For examplejQuery('create_event_dialog').dialog(...
in your case.You can find details on Using jQuery with Other Libraries.
Option 2:
You have to remove the Prototype libraries and instead include the jQuery libraries. You also need the jRails compatibility library to keep your prototype helpers. And you have to change your existing Prototype code to jQuery code.
For example your
Element.update
statement would look like this:See Using jQuery with Ruby on Rails for more details.
Careful: Depending on your existing code option 2 can be a lot of work, but will result in cleaner Javascript code. Also it might be worth considering switching to Rails 3.1 with UJS and jQuery as the official Javascript library.