Rails-AJAX模态对话框? [英] Rails - AJAX a Modal Dialog?
问题描述
我对学习如何AJAX模态对话框感兴趣.通常,如果我想向我的网站添加模式对话框,则在主JS文件中添加jquery UI对话框代码并将其绑定到ID.
I'm interested in learning how to AJAX a modal dialog. Typically if I wanted to add a modal dialog to my web site I added the jquery UI dialog code in my main JS file and binded it to an ID.
我相信我可以使用Rails创建一个Link,该Link从服务器获取所有对话框代码,然后打开对话框.
I believe with Rails I can create a Link, which fetches all the dialog code from the server and then opens the dialog.
这是真的吗?
如果是这样,您可以通过提供一个简单的示例说明所有东西在Rails MVC世界中所处的位置来帮助我理解吗?
If so, can you help me understand by providing a simple example of where everything lives in the rails MVC world?
谢谢
推荐答案
天哪,您在4个月前问过这个问题,所以您现在可能已经知道了.我也很难找到有关如何执行此操作的好文章.这是我想出的:
Gosh, you asked this 4 months ago so you've probably figured this out by now. I also had trouble finding a good write up on how to do this. Here's what I figured out:
在要弹出此对话框的任何页面中,您都希望具有这样的div(注意,您现在还不想显示此内容):
In whatever page you want to pull up this dialog box, you want to have a div like so (notice you don't want to display this yet):
<div id="person-form" title="Person" style="display:none"></div>
在该视图中,您还希望通过Ajax调用来弹出此对话框:
In that view, you also want an Ajax call to pull up this dialog box:
<%= link_to 'Edit Profile', edit_person_path(person), :remote => true %>
很显然,您希望此edit_person_path路由至某些操作.该操作应呈现具有类似以下内容(在jQuery中)的js.erb:
Obviously, you want this edit_person_path to route to some action. That action should render a js.erb that has something like the following (in jQuery):
$("#person-form").dialog({
autoOpen: true,
height: 600,
width: 600,
modal: true,
title: 'Edit Person',
buttons: {
"Edit": function() { $("#edit_person_<%= @person.id %>").submit() },
},
open: function() {
$("#person-form").html("<%= escape_javascript(render('form')) %>")
},
});
这会将部分_form.html.erb呈现到对话框中.
This will render a partial _form.html.erb into the dialog box.
注意:您还需要设置jQueryUI样式,以使对话框呈现美观.
Note: you're going to need to setup jQueryUI style as well for the dialog to render nice and pretty.
这篇关于Rails-AJAX模态对话框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!