有关RJS和link_to_remote用法的问题

时间:2009-11-05 03:08:46

标签: ruby-on-rails templates rjs

当我解决这个问题时,我正在我的网站上工作,我在我的博客中有100个帖子,我一次将它们分成10个,我显示第1个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

现在我想要的是页面底部的链接也要更新,以便下次用户点击更多时获取第3批,依此类推。基本上像

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

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

1 个答案:

答案 0 :(得分:2)

你非常接近。

使用(DOM_id,渲染选项)调用

replace_html。基本上你想渲染link_to_remote调用的输出。但是你没有以渲染可以使用的形式传递它。正如Barry Hess所指出的,替换更适合这项任务,因为你要改变的大多数是标签属性。

使用replace_html会导致嵌套标记,这可能会导致问题。您想要完全替换元素。替换具有与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}