Pagination in Rails With the will Paginate Bootstrap Gem

Pagination in Rails With the will Paginate Bootstrap Gem

23 Feb 2018
0 Comments
Pagination in Rails With the will Paginate Bootstrap Gem

To divide a large number of records into multiple parts, we use pagination. It allows users to display a part of records only. Loading all records on a single page may take time, so it is always recommended to created pagination. In will_paginate-bootstrap gem integrates the Bootstrap pagination component. The usage for this is extremely simple and it magically works. So let’s take a look at adding this.

STEP 1. gem install will_paginate-bootstrap


STEP 2. Open app/controller/posts_controller.rb and it adds necessary parameters in the resultant collection of records to display pagination links in the front-end (.erb).
 


Parameters:

  • : page- This is a parameter sent in Query String. Based on this, which records are to be fetched is decided. i.e. Offset is decided:per_page- This is the Number of results that you want to fetch per page i.e. From offset (.erb)
  • limit – per page limit for the paginated result data
  • offset – current paginated data set -> to show the current page

 

STEP 3. Open app/views/posts/index.html.erb for adding pagination links and also add bootstrap CDN links.

It will add pagination links like,  <-  Older 1 2 Newer->  on front-end.
 

STEP 4.  A collection of extensions for the database layer that enable paginated queries, and view helpers for popular frameworks that render pagination links. With the proper combination of view helpers and CSS styling.

The number of Pages here 1, 2 is decided based on the total numbers of records that you have in your database, the final result can look like this:

PAGE NO 1. (Paginated result data)

PAGE NO 2. (Paginated result data)




Leave a comment: