This Tip Of The Day (TOTD) explains how to add pagination to your Rails application.
- Create a simple Rails scaffold as:
~/samples/jruby >~/tools/jruby/bin/jruby -S rails paginate
~/samples/jruby/paginate >~/tools/jruby/bin/jruby script/generate scaffold book title:string author:string
~/samples/jruby/paginate >sed s/’adapter: sqlite3′/’adapter: jdbcsqlite3′/ <config/database.yml >config/database.yml.new
~/samples/jruby/paginate >mv config/database.yml.new config/database.yml
~/samples/jruby/paginate >~/tools/jruby/bin/jruby -S rake db:migrate - Edit “test/fixtures/books.yml” and specify the content as:
# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html one:
title: Ultramarathon Man Confessions of an All-Night Runner
author: Dean Karnazestwo:
title: My Life on the Run
author: Bart Yassothree:
title: 50/50 Secrets I Learned Running 50 Marathons in 50 Days
author: Dean Karnazesfour:
title: Born to Run
author: Christopher Mcdougallfive:
title: Four Months to a Four-hour Marathon
author: Dave Kuehlssix:
title: Galloway’s Book on Running
author: Jeff Gallowayseven:
title: Marathoning for Mortals
author: John Bingham and Jenny Hadfieldeight:
title: Marathon You Can Do It!
author: Jeff Gallowaynine:
title: Marathon The Ultimate Training Guide
author: Hal Higdonten:
title: Running for Mortals
author: John Bingham and Jenny Hadfieldand load the fixtures as:
~/samples/jruby/paginate >~/tools/jruby/bin/jruby -S rake db:fixtures:load
(in /Users/arungupta/samples/jruby/paginate) - Run the application as:
~/samples/jruby/paginate >~/tools/jruby/bin/jruby -S glassfish -l
Starting GlassFish server at: 129.145.132.8:3000 in development environment…
Writing log messages to: /Users/arungupta/samples/jruby/paginate/log/development.log.. . .
Jul 29, 2009 2:06:44 PM com.sun.grizzly.scripting.pool.DynamicPool$1 run
INFO: New instance created in 7,488 millisecondsThe application is accessible at “http://localhost:3000/books” and looks like:

The page shows 10 rows, all in one page.
- Lets add pagination to this simple sample.
- Install will_paginate gem as:
/tools/jruby >./bin/jruby -S gem install will_paginate
JRuby limited openssl loaded. gem install jruby-openssl for full support.
http://wiki.jruby.org/wiki/JRuby_Builtin_OpenSSL
Successfully installed will_paginate-2.2.2
1 gem installed
Installing ri documentation for will_paginate-2.2.2…
Installing RDoc documentation for will_paginate-2.2.2…There are other methods of installation as well.
- Edit “config/environment.rb” and add
require “will_paginate” as the last line.
- Edit the “index” action in “app/controllers/books_controller.rb” as:
@books = Book.paginate(:page => params[:page], :per_page => 5)
#@books = Book.all“:per_page” specifies the number of items to be displayed in each page.
- In “app/views/books/index.html.erb”, add:
<%= will_paginate @books %> right after “</table>”.
The output now looks like:

and clicking on “Next” shows:

The information is nicely split amongst 2 pages.
An important point to remember is that will_paginate only adds pagination to your Rails app. You are still required to display all the values.
But essentially replacing “@books = Book.all” with “@books = Book.paginate(:page => params[:page], :per_page => 5)” in the Controller and adding
“<%= will_paginate @books %>” did the trick for us.
Clean and simple!
Please leave suggestions on other TOTD (Tip Of The Day) that you’d like to see. A complete archive of all the tips is available here.
Technorati: jruby rubyonrails glassfish pagination will_paginate
Related posts:
Hi,
I am new to rails I’m trying to use will_paginate for my new rails application which uses two tables with an foreign key connection.But when i try to run that I’m getting NoMethodError in the view page telling that unknown method for the column name which I’m trying to display and that column is taken from the other table.
Thanks for any help
Comment by Ashwin Kumar — October 12, 2009 @ 11:53 am