Tips: Multiple pagination with will_paginate

Will Paginate is a great and widely used gem for pagination in Ruby on Rails.

Sometimes you need to paginate multiple list on the same page.

This tip show you how to put a list of paginated products and a list of of paginated services on the same page with will_paginate.
The key point is to use a custom param param_name in the view and also in the controller

The view code is something like this:

1
2
3

# ...
# products list here
<%= will_paginate @products, :param_name => 'products_page', :prev_label => t('helpers.labels.previous'), :next_label => t('helpers.labels.next') %>

1

1
2
3

# services list here
<%= will_paginate @services, :param_name => 'services_page', :prev_label => t('helpers.labels.previous'), :next_label => t('helpers.labels.next') %>
# ...

And this is the code in the controller

1
2
3
4
5
6

# ...
def index
@products = current_company.products.paginate(:page => params[:products_page], :per_page => 10)
@services = current_company.products.paginate(:page => params[:services_page], :per_page => 10)
end
# ...

Save more time and effort by using this free and complete designer checklist.