Rails3:如何将参数传递给自定义 will_paginate 渲染器? [英] Rails3: How to pass param into custom will_paginate renderer?

查看:48
本文介绍了Rails3:如何将参数传递给自定义 will_paginate 渲染器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've got a custom will_paginate renderer that overrides WillPaginate::ViewHelpers::LinkRenderer's link method like so:

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

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

...and that works great, except you can see the hard-coded 95 in that link. How would I pass a parameter (e.g. user or user's ID) into the custom renderer via the Rails view?

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

或者是我遗漏了什么,有什么更简单的方法吗?

Or is there something I'm missing, some easier way to do it?

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

BTW: @user_friends isn't available in the custom renderer, and I've already tried just adding params onto the end of that will_paginate call, e.g. :user => user)

推荐答案

查看:

<%= 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

Note that this works for the will-paginate version: 2.3.6

这篇关于Rails3:如何将参数传递给自定义 will_paginate 渲染器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆