Pagination with Kaminari

#73 Pagination with Kaminari
3/26/2017

Summary

When displaying a significant number of records, it is often a basic functions in a web application is to paginate the records and load them as requested.
3
rails view 2:37 min

Summary

Gemfilegem 'kaminari'
Terminal# Generate the kaminari config
rails g kaminari:config

# Generate kaminari views
rails g kaminari:views bootstrap3
users_controller.rb  def index
    @users = User.order(:last_name).page(params[:page])
  end
index.html.erb<%= paginate @users %>
user.rbclass User < ApplicationRecord
  paginates_per 5
end


Photo
Pavan Prakash CS said 3 months ago:

Thanks for helping the starters like me with your great work. Can you please do a video on infinite scroll with kaminari? The example used in the wiki of the gem is very old and the JavaScript library is not maintained anymore. 

635114?v=3&s=64
kobaltz said 3 months ago:

Have a look at https://www.driftingruby.com/episodes/infinite-scrolling

It uses `will_paginate` but `kaminari` should be a fairly simple drop in with some minor changes.

Photo
Pavan Prakash CS said 3 months ago:

Thanks for the reply. Thumbs up for all your help. 

7865030?v=3&s=64
Schwad said 3 months ago:

That's really cool, fired up a toy app just to play with Kaminari following this. Thanks for the screencast!

Login to Comment
Pagination is simply a way that you can load a subset of records and then give the user an option and visibility to see the total number of records and how many pages there are and to navigate through them. So, we will be building an application to function similar to the image below where we have our list of users and we can navigate between the different pages of users at the bottom with the pagination links. So to get started, at the following to the Gemfile, be sure to run bundle and restart your Rails application.
gem 'kaminari'
Within the terminal, you can run rails g kaminari:config which will generate an initializer file which will show some of the default settings of Kaminari. This file will be generated in config/initializers/kaminari.rb. Then within the users_controller.rb, we can update the index action to look something like this.
def index
        @users = User.order(:last_name).page(params[:page])
      end
Kaminari provides a page scope which takes in a parameter. This will create the SQL offset to return that subset of records. Kaminari also has a per scope which will be the number of records that will be returned (SQL equivalent of LIMIT). Then within the index.html.erb we can add in
<%= paginate @users %>
which will generate the pagination links. You're not limited to the number of times you can render this helper so it may be beneficial to have it at the top of the page as well as the bottom; depending on the number of records you're returning. Since the application is using Bootstrap 3, we can use the Kaminari Themes to style our pagination links. In the console, you can run the below command to generate the views for the pagination links. This will include the necessary classes around the pagination links so that it will automatically pick up the styling of your CSS Framework.
rails g kaminari:views bootstrap3
If you find yourself in a situation where you have a particular model that you wish to have a different default limit of the number of records, you can add the paginates_per helper in the model.
class User < ApplicationRecord
        paginates_per 5
      end