TOTD #88: How add pagination to Rails - will_paginate


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
Comments:

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

Posted by Ashwin Kumar on October 12, 2009 at 04:53 AM PDT #

Post a Comment:
Comments are closed for this entry.
About

profile image
Arun Gupta is a technology enthusiast, a passionate runner, author, and a community guy who works for Oracle Corp.


Java EE 7 Samples

Stay Connected

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today