Skip to content
Generic pagination support for Ruby
Find file
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
lib Use minitest/spec, support each.with_index Jul 1, 2013
spec Use minitest/spec, support each.with_index Jul 2, 2013
.gitignore Cleanup files Jul 1, 2013
.ruby-version Modernize gem build & fix tests Jul 1, 2013
Gemfile Modernize gem build & fix tests Jul 2, 2013
LICENSE.txt Update docs Jul 1, 2013
Rakefile Use minitest/spec, support each.with_index Jul 2, 2013
paginator.gemspec Remove rake gem dep Jul 1, 2013


Paginator is a simple pagination class that provides a generic interface suitable for use in any Ruby program.

Paginator doesn't make any assumptions as to how data is retrieved; you just have to provide it with the total number of objects and a way to pull a specific set of objects based on the offset and number of objects per page.


In both of these examples I'm using a PER_PAGE constant (the number of items per page), but it's merely for labeling purposes.

You could, of course, just pass in the number of items per page directly to the initializer without assigning it somewhere beforehand.

In Plain Ruby

bunch_o_data = (1..60).to_a
pager = Paginator.create(bunch_o_data.size, PER_PAGE) do |offset, per_page|
pager.each do |page|
  puts "Page ##{page.number}"
  page.each do |item|
    puts item

In Rails

Nothing changes with Paginator; you just use ActiveRecord from within the block you provide.

A controller action:

def index
  @pager = Paginator.create(Foo.count, PER_PAGE) do |offset, per_page|
  @page =[:page])

In your view:

<% @page.each do |foo| %>
  <%# Show something for each item %>
<% end %>
<%= @page.number %>
<%= link_to("Prev", foos_url(page: @page.prev.number)) if @page.prev? %>
<%= link_to("Next", foos_url(page: if %>


See LICENSE.txt.

Something went wrong with that request. Please try again.