Pagination in Rails With the will_paginate-bootstrap Gem
To divide large number of records into multiple parts, we use pagination. It allows user to display a part of records only. Loading all records in 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 record to display pagination links in front-end (.erb).
- :page- This is 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 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 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 proper combination of view helpers and CSS styling.
Number of Pages here 1, 2 is decided based on 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)