Miles to go …

August 3, 2009

TOTD #88: How add pagination to Rails – will_paginate

Filed under: rails, totd — arungupta @ 3:00 am

This Tip Of The Day (TOTD) explains how to add pagination to your Rails application.

  1. 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

  2. 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 Karnazes

    two:
      title: My Life on the Run
      author: Bart Yasso

    three:
      title: 50/50 Secrets I Learned Running 50 Marathons in 50 Days
      author: Dean Karnazes

    four:
      title: Born to Run
      author: Christopher Mcdougall

    five:
      title: Four Months to a Four-hour Marathon
      author: Dave Kuehls

    six:
      title:  Galloway’s Book on Running
      author: Jeff Galloway

    seven:
      title: Marathoning for Mortals
      author: John Bingham and Jenny Hadfield

    eight:
      title:  Marathon You Can Do It!
      author: Jeff Galloway

    nine:
      title: Marathon The Ultimate Training Guide
      author: Hal Higdon

    ten:
      title: Running for Mortals
      author: John Bingham and Jenny Hadfield

    and load the fixtures as:

    ~/samples/jruby/paginate >~/tools/jruby/bin/jruby -S rake db:fixtures:load
    (in /Users/arungupta/samples/jruby/paginate)

  3. 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 milliseconds

    The application is accessible at “http://localhost:3000/books” and looks like:

    The page shows 10 rows, all in one page.

  4. Lets add pagination to this simple sample.
    1. 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.

    2. Edit “config/environment.rb” and add
      require “will_paginate”

      as the last line.

    3. 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.

    4. 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

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • StumbleUpon
  • Technorati
  • Twitter
  • Slashdot
Related posts:
  1. TOTD #28: Getting Started with Rails 2.0 Scaffold
  2. TOTD #110: JRuby on Rails application using Oracle on GlassFish
  3. TOTD #89: How to add pagination to an Apache Wicket application
  4. jMaki on Rails – Reloaded for NetBeans 6.1 beta & Rails 2.0
  5. TOTD #44: JDBC Connection Pooling for Rails on GlassFish v3

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress