Rails3: How to pass param into custom will_paginate renderer?

南楼画角 提交于 2019-12-21 03:42:56

问题


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

...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?

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)


回答1:


View:

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

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




回答2:


will_paginate lets you pass in :params for the links:

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


来源:https://stackoverflow.com/questions/6834546/rails3-how-to-pass-param-into-custom-will-paginate-renderer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!