Skip to main content

TOTD #88: How add pagination to Rails - will_paginate

Posted by arungupta on August 3, 2009 at 9:27 AM PDT



This Tip style="font-weight: bold;">Of style="font-weight: bold;">The style="font-weight: bold;">Day (TOTD) explains
how to add pagination to your Rails application.

  1. Create a simple Rails scaffold as:


    style="text-align: left; background-color: rgb(204, 204, 255); width: 100%;"
    cellpadding="2" cellspacing="2">
    ~/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:


    style="text-align: left; background-color: rgb(204, 204, 255); width: 100%;"
    cellpadding="2" cellspacing="2">
    #
    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:


    style="text-align: left; background-color: rgb(204, 204, 255); width: 100%;"
    cellpadding="2" cellspacing="2">
    ~/samples/jruby/paginate
    >~/tools/jruby/bin/jruby
    -S rake db:fixtures:load


    (in /Users/arungupta/samples/jruby/paginate)


  3. Run the application as:


    style="text-align: left; background-color: rgb(204, 204, 255); width: 100%;"
    cellpadding="2" cellspacing="2">
    ~/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:



    alt=""
    src="http://blogs.sun.com/arungupta/resource/ror/paginate-books-output-no-pagination.png">



    The page shows 10 rows, all in one page.

  4. Lets add pagination to this simple sample.
    1. Install href="http://wiki.github.com/mislav/will_paginate">will_paginate
      gem as:


      style="text-align: left; background-color: rgb(204, 204, 255); width: 100%;"
      cellpadding="2" cellspacing="2">
      /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 href="http://wiki.github.com/mislav/will_paginate/installation">other
      methods of installation as well.

    2. Edit "config/environment.rb" and add


      style="text-align: left; background-color: rgb(204, 204, 255); width: 100%;"
      cellpadding="2" cellspacing="2">
      require
      "will_paginate"



      as the last line.

    3. Edit the "index" action in
      "app/controllers/books_controller.rb" as:


      style="text-align: left; background-color: rgb(204, 204, 255); width: 100%;"
      cellpadding="2" cellspacing="2">
      @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:


      style="text-align: left; background-color: rgb(204, 204, 255); width: 100%;"
      cellpadding="2" cellspacing="2">
      <%=
      will_paginate @books %>



      right after "</table>".



      The output now looks like:



      alt=""
      src="http://blogs.sun.com/arungupta/resource/ror/paginate-books-output-page1.png">



      and clicking on "Next" shows:



      alt=""
      src="http://blogs.sun.com/arungupta/resource/ror/paginate-books-output-page2.png">



      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 href="http://blogs.sun.com/arungupta/tags/totd">here.




Technorati: jruby
rubyonrails
glassfish
pagination
will_paginate

Related Topics >>