Ruby On Rails Classroom image

Pooja  Negi / Student / 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
Pooja   Negi

Skills    Ruby On Rails

Qualifications :- High School - SSN high school, College/University - HNBGU, College/University - SRHU,
Location :-Ranipokhari,Rishikesh,Uttarakhand,India
Description:- Student
Explore
 

  Students (0)