Skip to Content
Professional Ruby on Rails™
book

Professional Ruby on Rails™

by Noel Rappin
February 2008
Intermediate to advanced
479 pages
14h
English
Wrox
Content preview from Professional Ruby on Rails™

5.4. Pagination

If you've used Google, you've seen pagination in action. The first 25 or so search hits are displayed, followed by some navigation to allow you to move between pages. On the back end, the server knows what page you are looking for and serves up the correct set of results for that page.

In the Rails 1.x days, pagination was a helper method and part of the Rails core. But it turned out that a lot of people didn't much care for the implementation, and the pagination helper was removed from Rails 2.0 as part of the general shedding of non-core functionality. There's no clear market leader in its place, but there are a couple of plugin contenders worth looking at. Keep an eye out for new developments in this space.

5.4.1. will_ paginate

The first contender is the will_paginate plugin available from the folks at Err Free consulting. You can get the plugin from here:

$ ruby script/plugin install svn://errtheblog.com/svn/plugins/will_paginate

This plugin allows you to use the paginate method in any place where you would use the method find(:all). Here is an example of the simplest use of this method:

@recipes = Recipe.paginate(:page => params[:page])

The page option tells the plugin which page of data to retrieve, and must be specified on each call to paginate. The default is to put 30 items on the page. If your class responds to a per_page class method, the results of that method are used, so you can specify that as follows:

def self.per_page
  10
end

Any options you send ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Rails 4 Test Prescriptions

Rails 4 Test Prescriptions

Noel Rappin
Ruby on Rails® Bible

Ruby on Rails® Bible

Timothy Fisher

Publisher Resources

ISBN: 9780470223888Purchase book