在 Rails 中添加模式对话框?
我想将 Ajax 链接添加到我的应用程序中。当我单击链接时,它应该弹出一个模式对话框,其中包含事件的详细信息。 我在我的助手类中添加了这个链接。
link_to(event.name, events_path(@event), :remote => true
然后我创建 js 文件将内容插入到隐藏的 div 中。
$('.modal').html(<%= escape_javascript(render(@event)) %>);
$('.modal').dialog();
这里的模态是我隐藏的 div。但它无法弹出任何模式对话框。我无法理解错误是什么以及为什么它不起作用。请问有人可以帮我纠正这个问题吗?
I want to add a Ajax link into my application. When I click on the link it should pop up a modal dialog which contains event's details..
I add this link in my helper class.
link_to(event.name, events_path(@event), :remote => true
Then I create js file to insert content into hidden div.
$('.modal').html(<%= escape_javascript(render(@event)) %>);
$('.modal').dialog();
In here modal is my hidden div. But it could not pop up any modal dialog. I cannot understand what is the error and why is it not working. Plz can anybody help me to correct this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将
$('.modal').html(<%= escape_javascript(render(@event)) %>);
更改为
$('.modal').html(" <%= escape_javascript(render(@event)) %>");
从 JS 的角度来看,您的代码将无效,因为您没有将渲染用引号括起来,并且它将尝试评估你的 HTML。
编辑
如果您尝试通过
show
点击链接到此内容,则需要使用show.js.erb
来显示您的模式对话框而不是create.js.erb
。仅当您将表单POST
到/events
时才会调用create
,而这里看起来您只是试图显示该表单的详细信息事件。将上面的代码(带引号)放入
show.js.erb
中,确保您的show
方法中有一个respond.js
响应控制器上,然后重试。Change
$('.modal').html(<%= escape_javascript(render(@event)) %>);
to
$('.modal').html("<%= escape_javascript(render(@event)) %>");
From a JS point of view, your code will be invalid because you're not wrapping your render in quotes and it will try to evaluate your HTML.
EDIT
If you're trying to link to this on a
show
click, you'll need to useshow.js.erb
to show your modal dialog rather thancreate.js.erb
.create
will only be called if youPOST
a form to/events
whereas here it looks like you're trying to show just the details of the event.Put the code above (with quotes) in the
show.js.erb
, make sure you have arespond.js
response in yourshow
method on the controller, and try it again.可能您没有安装 Jquery,因为 Rails 默认是 Prototype 库。我会升级到 Rails 3.1,这样可以轻松使用 jquery:
rails new example -j jquery
或安装 jquery:
http://railscasts.com/episodes/136-jquery
Could be possible that you didnt install Jquery since the rails default is Prototype library. I would upgrade to Rails 3.1 which makes it easy to use jquery:
rails new example -j jquery
or install jquery:
http://railscasts.com/episodes/136-jquery