在 Ruby on Rails 中使用 rjs 实现 AJAX

发布于 2024-10-19 19:12:01 字数 214 浏览 2 评论 0原文

假设您想使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

可爱咩 2024-10-26 19:12:01

page.insert_html 不考虑 HTML 的原始源是否是部分的 - 您只需要给它一个 DOM ID(即

;) 用作插入的参考点。

如果您知道要通过 AJAX 在何处插入 HTML,请让该元素具有 id 并将其用作 page.insert_html 的参数


。下面是一个示例。假设您有一个呈现以下内容的视图:

<div id="div_tag_in_home_page">
  <%= render :partial => "shared/container" %>
</div>

以及这些部分:

<!-- app/views/shared/_container.html.erb -->
<div id="container_div">
  <%= render :partial => "shared/sub_container" %>
</div>

<!-- app/views/shared/_sub_container.html.erb -->
<div id="sub_container_div">
  Some Content That Needs Appending
</div>

在呈现时,将为您提供以下内容:

<div id="div_tag_in_home_page">
  <!-- app/views/shared/_container.html.erb -->
  <div id="container_div">
    <!-- app/views/shared/_sub_container.html.erb -->
    <div id="sub_container_div">
      Some Content That Needs Appending
    </div>
  </div>
</div>

使用名为 app/views/shared/ 的部分将内容添加到 #sub_conainer_div _additional_content.html.erb,您可以使用:

page.insert_html :bottom, 'sub_container_div', :partial => 'shared/additional_content'

page.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 to page.insert_html


Here's an example. Assuming that you have a view that renders the following:

<div id="div_tag_in_home_page">
  <%= render :partial => "shared/container" %>
</div>

And these partials:

<!-- app/views/shared/_container.html.erb -->
<div id="container_div">
  <%= render :partial => "shared/sub_container" %>
</div>

<!-- app/views/shared/_sub_container.html.erb -->
<div id="sub_container_div">
  Some Content That Needs Appending
</div>

Which, when rendered, gives you this:

<div id="div_tag_in_home_page">
  <!-- app/views/shared/_container.html.erb -->
  <div id="container_div">
    <!-- app/views/shared/_sub_container.html.erb -->
    <div id="sub_container_div">
      Some Content That Needs Appending
    </div>
  </div>
</div>

To add content to #sub_conainer_div using a partial named app/views/shared/_additional_content.html.erb, you would use:

page.insert_html :bottom, 'sub_container_div', :partial => 'shared/additional_content'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文