2

当我遇到这个问题时,我正在我的网站上工作,我的博客中有 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'}

关于我该怎么做的任何线索?

4

1 回答 1

2

你很亲密。

replace_html 使用 (DOM_id, options for render) 调用。本质上,您想要呈现 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'}

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

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}
于 2009-11-05T07:09:05.007 回答