12

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)

yalestar
  • 9,334
  • 6
  • 39
  • 52

2 Answers2

23

will_paginate lets you pass in :params for the links:

will_paginate(@user_friends, :params => { :user_id => 95 })
Henrik N
  • 15,786
  • 5
  • 82
  • 131
  • Do you know any way to pass variable instead of integer? For example, value of a text_field? – Gokhan Arik Jan 02 '15 at 20:19
  • @GokhanArik What do you mean? You should be able to do `will_paginate(@user_friends, :params => { :anything => "hello" })` and then the pagination links will be something like "/friends?page=2&anything=hello". Then the friends page could set the value of a text field to `params[:anything]` if it wants to. – Henrik N Jan 03 '15 at 12:00
  • Instead of "hello", I want to pass value of f.text_field. Is that possible? You can look at the question I posted - http://stackoverflow.com/questions/27747939/passing-text-field-value-to-will-paginate-as-parameter-in-rails – Gokhan Arik Jan 03 '15 at 21:05
4

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

Arun Kumar Arjunan
  • 6,827
  • 32
  • 35
  • hi please check my requirement here http://stackoverflow.com/questions/23755799/how-to-pass-model-params-with-will-paginate-in-rails3 . Please help me – santosh May 22 '14 at 05:41