在 Ruby on Rails 中使用 rjs 实现 AJAX
假设您想使用 rjs 添加一些 ajax,并且您想要异步添加 html 的元素是一个部分中的一个部分,这意味着您的主视图在共享文件夹中呈现一个部分,然后该部分在另一个文件夹中呈现另一个部分。您的共享文件夹。当您调用 page.insert_html :bottom, :partial =>...
时,如何在 .rjs 文件中解释这一点,以及如何在主视图和两个部分视图中解释这一点?
So let's say you want to add some ajax with rjs and the element that you want to asynchronously add html to is a partial within a partial, meaning that your home view renders a partial in your shared folder, and then that partial renders another partial in your shared folder. How do you account for this in your .rjs file when you call page.insert_html :bottom, :partial =>...
and also how do you account for this in your home view and both partials?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
page.insert_html
不考虑 HTML 的原始源是否是部分的 - 您只需要给它一个 DOM ID(即;
) 用作插入的参考点。如果您知道要通过 AJAX 在何处插入 HTML,请让该元素具有
id
并将其用作page.insert_html
的参数。下面是一个示例。假设您有一个呈现以下内容的视图:
以及这些部分:
在呈现时,将为您提供以下内容:
使用名为
app/views/shared/ 的部分将内容添加到
,您可以使用:#sub_conainer_div
_additional_content.html.erbpage.insert_html
doesn't consider whether the original source of the HTML was a partial or not - you only need to give it a DOM ID (ie<div id="foo">
) for it to use as the reference point for the insertion.If you know where you want to insert the HTML via AJAX, make the element has an
id
and use it as an argument topage.insert_html
Here's an example. Assuming that you have a view that renders the following:
And these partials:
Which, when rendered, gives you this:
To add content to
#sub_conainer_div
using a partial namedapp/views/shared/_additional_content.html.erb
, you would use: