关于 RJS 和 link_to_remote 使用的问题

发布于 2024-08-09 20:20:20 字数 715 浏览 7 评论 0原文

当我遇到这个问题时,我正在我的网站上工作,我的博客中有 100 个帖子,我一次对它们分页 10 个,我显示第一个 15 并在底部显示一个链接,该链接基本上是使用 link_to_remote 标签实现的。

<%= link_to_remote "More Posts", :url => {:action => 'view' ,:id => link.to_i + 1} , :html => {:id => 'more-link'} %>

我点击它并获取接下来的 15 篇文章,并通过 insert_html 将其附加到包含前 15 篇文章的容器

page.insert_html :bottom, :puzzles , :partial => 'puzzles', :object => @puzzles

现在我想要的是页面底部的链接也进行更新,以便用户下次单击更多内容第三批已获取,依此类推。基本上类似于

page.replace_html 'more-link', link_to_remote "More Posts", :url => {:action => 'view' ,:id => link.to_i + 1} , :html => {:id => 'more-link'}

关于我如何做到这一点的任何线索?

I was working on my website when I cam across this problem, I have 100 posts in my blog and I am paginating them 10 at a time, I display the 1st 15 and display a link at the bottom which basically is implemented using the link_to_remote tag.

<%= link_to_remote "More Posts", :url => {:action => 'view' ,:id => link.to_i + 1} , :html => {:id => 'more-link'} %>

I click on that and get the next 15 posts, and append it to the container containing the 1st 15 posts through insert_html

page.insert_html :bottom, :puzzles , :partial => 'puzzles', :object => @puzzles

Now what I want is the link at the bottom of the page to update too, so that the next time the user clicks more the 3rd batch is fetched and so on. Basically something like

page.replace_html 'more-link', link_to_remote "More Posts", :url => {:action => 'view' ,:id => link.to_i + 1} , :html => {:id => 'more-link'}

Any clues as to how I can do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

绅刃 2024-08-16 20:20:20

你们非常接近。

使用(DOM_id,渲染选项)调用replace_html。本质上,您想要呈现 link_to_remote 调用的输出。但是您没有以渲染可以使用的形式传递它。正如 Barry Hess 指出的那样,替换更适合此任务,因为您要更改的大部分内容是标签属性。

使用replace_html会导致嵌套标签,这可能会导致问题。您想要完全替换该元素。 Replace 与replace_html 具有相同的语法,因此如果您只是切换replace_html 进行替换,您会遇到相同的问题。

这就是您想要的:

page.replace 'more-link', :text => link_to_remote "More Posts", 
  :url => {:action => 'view' ,:id => link.to_i + 1} ,
  :html => {:id => 'more-link'}

但是我不确定 link_to_remote 是否可以从 RJS 访问。如果上述方法不起作用,您可以随时执行以下操作:

page.replace 'more-link', :inline => "<%= link_to_remote 'More Posts', 
  :url => {:action => 'view' ,:id => link.to_i + 1},
  :html => {:id => 'more-link'} %>", :locals => {:link => link}

You're very close.

replace_html is called with (DOM_id, options for render). Essentially you want to render the output of a link_to_remote call. But you're not passing it in a form that render can use. As Barry Hess points out replace is much better suited for this task, because most of what you're changing is the tag attributes.

Using replace_html would result in nested tags which is could cause problems. You want to replace the element entirely. Replace has the same syntax of replace_html so you would run into the same problems if you were just switch replace_html to replace.

Here's what you want:

page.replace 'more-link', :text => link_to_remote "More Posts", 
  :url => {:action => 'view' ,:id => link.to_i + 1} ,
  :html => {:id => 'more-link'}

However I'm not sure if link_to_remote is accessible from RJS. If the above doesn't work you could always do this:

page.replace 'more-link', :inline => "<%= link_to_remote 'More Posts', 
  :url => {:action => 'view' ,:id => link.to_i + 1},
  :html => {:id => 'more-link'} %>", :locals => {:link => link}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文