Rails3:如何将参数传递到自定义will_paginate渲染器中?

12

我有一个自定义的will_paginate渲染器,覆盖了WillPaginate :: ViewHelpers :: LinkRenderer的链接方法,代码如下:

   def link(text, target, attributes = {})
      "<a href='/users/95/friends_widget?page=#{target}' rel='next' data-remote='true'>#{text}</a>"
   end

...这很好,但在链接中可以看到硬编码的95。我如何通过Rails视图将参数(例如用户或用户ID)传递到自定义渲染器中?

<%= will_paginate(@user_friends, :remote => true, :renderer => FriendsRenderer) %>

还是有什么我没注意到的东西吗?有更简单的方法吗?

顺便说一下:@user_friends 在自定义渲染器中不可用,我已经尝试在will_paginate调用的末尾添加参数,例如::user => user)

2个回答

23

will_paginate 允许你为链接传递 :params 参数:

will_paginate(@user_friends, :params => { :user_id => 95 })

你知道有什么方法可以传递变量而不是整数吗?例如,文本字段的值? - Gokhan Arik
@GokhanArik 你的意思是什么?你应该能够执行 will_paginate(@user_friends, :params => { :anything => "hello" }),然后分页链接将会是类似于 "/friends?page=2&anything=hello" 的形式。接下来,如果好友页面需要的话,它可以将文本字段的值设置为 params[:anything] - Henrik N
您IP地址为143.198.54.68,由于运营成本限制,当前对于免费用户的使用频率限制为每个IP每72小时10次对话,如需解除限制,请点击左下角设置图标按钮(手机用户先点击左上角菜单按钮)。 - Gokhan Arik

4

视图:

<%= will_paginate @user_friends, :renderer => 'FriendsRenderer',
                         :remote => true,
                         :link_path => friends_widget_user_path(@user) %>

class FriendsRenderer < WillPaginate::LinkRenderer
  def prepare(collection, options, template)
    @link_path = options.delete(:link_path)
    super
  end

protected
  def link(page, text, attributes = {})
    # Here you can use @link_path   
  end
end

请注意,此方法适用于will-paginate版本为2.3.6。

嗨,请在此处检查我的要求:http://stackoverflow.com/questions/23755799/how-to-pass-model-params-with-will-paginate-in-rails3。请帮助我。 - santosh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接