Ruby On Rails Classroom image

Anil  Bist / Professional / Web Technology

 
To post your Question Join Classroom
 
Lesson Topics's No:-  First|1 | 2 | 3 | 4 | 5|Last
Lessons:-Pagination

To get pagination working, we need to add some codeto theindex view telling Rails to paginate the users, and we need to replace User.all in the index action with an object that knows about pagination. We’ll start by adding the special will_paginate method in the view (Listing 9.34); we’ll see in a moment why the code appears both above and below the user list.

 

 

The will_paginate method is a littlemagical; inside a users view, it automatically looks for an @users object, then displays pagination links to access other pages. The view in Listing 9.34 doesn’t work yet, though, because currently @users contains the results of User.all (Listing 9.24), which is of class Array, whereas will_paginate expects an object of class ActiveRecord::Relation. Happily, this is just the kind of object returned by the paginate method added by the will paginate gem to all Active Record objects:

 

 

Note that paginate takes a hash argument with key :page and value equal to the page requested. User.paginate pulls the users out of the database one chunk at a time (30 by default), based on the :page parameter. So, for example, page 1 is users 1–30, page 2 is users 31–60, etc. If the page is nil, paginate simply returns the first page.

Using the paginate method, we can paginate the users in the sample application by using paginate in place of all in the index action (Listing 9.35). Here the :page parameter comes from params[:page], which is generated automatically by will_paginate.

 
 
 
image
Anil  Bist

Skills    Ruby On Rails

Qualifications :- High School - SLV, College/University - Graphic Era Deemed Univ University,
Location :-Dehradun,Dehradun,Uttarakhand,India
Description:-

I started my Professional Journey in 2006 with one of the Web Development Company in Bangalore and my 1st framework was "Ruby on Rail" as Web development and delivered around 5+ Projects using this platform. Then came another dimension as JEE/Sturst framework, Gradually I realized that I want to build something on my own and give my passion and energy on creating something different a
Explore

 

  Students (0)