Skip to content
Menu
Railshero
  • Blog
  • About Me
  • Contact Me
  • Privacy Policy
Railshero
Adding Pagination With Kaminari

Adding Pagination With Kaminari

Posted on July 16, 2021July 16, 2021 by Rails Hero

Why Pagination ?

As our list of record accumulates, we would not want to show the entire list of records in a single page, as this is impractical and will cause our rails app to get slower and slower and as well as degrade the performance of our database server. So, an easy way to fix that issue is to add pagination to our rails apps. This will limit the number of records that we want to show in a single page. We can also specify the number of records that we want to add per page, and as well as customize the look of pagination.

Pagination gems

There are a lot of gems that you can use to add pagination to your app, gems like kaminari, will_paginate, pagy, etc. Here i’ll show you how to do it with kaminari. If you have a rails application with huge number of records for example a million records or more, consider using pagy instead of kaminari or will_paginate, as it is gives better performance. But for small applications kaminari is fine.

  • Kaminari Github Link: https://github.com/kaminari/kaminari
  • Will Paginate Github Link: https://github.com/mislav/will_paginate
  • Pagy Github Link: https://github.com/ddnexus/pagy

Adding Pagination With Kaminari

Gemfile

To add pagination with Kaminari, first add kaminari to your gemfile and run bundler

#File: Gemfile
gem 'kaminari'

Controller

In your controller add the kaminari scope, in the method that you want pagination, for instance i added it in my index method. You can limit the number of records that you want per page, for instance if i want 10 records to show per page then i would add .page(params[:page]).per(10)

#File: app/controllers/book_controller.rb
class BooksController < ApplicationController
   def index
   @books = Book.order("created_at DESC").page(params[:page]).per(10)
   end
end

Views

Kaminari also comes with it’s own views, to generate those views simply run the following command, this will generate kaminari views with the default theme

rails g kaminari:views default

You can also generate kaminari views with other themes, for eg bootstrap4. Checkout kaminari themes here: https://github.com/amatsuda/kaminari_themes

rails g kaminari:views bootstrap4

You can also customize the views generated by kaminari in app/views/kaminari/

After that, add kaminari to your views, for example this is index view of my book records

#File: app/views/books/index.html.erb
<div class="pagination">
<%= paginate @books %>
</div>

kaminari pagination

More on Pagination

Kaminari supports other template engines as well (haml, slim, etc). You can also generate a kaminari config, to configure kaminari as per your specific requirements or configure it all from one place, checkout kaminari docs for more info: https://www.rubydoc.info/gems/kaminari/0.17.0

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Tags

active storage activity logging administration admin panel analyze assets assign to many attachment attachments authentication authorization avatar bootstrap cdn deployments devise dislikes environment variables file uploads file validation Gemfile gems heroku index indexing initializer javascript likes links pagination parameters pipeline production routes.rb ruby gem ruby gems search sendgrid smtp spectrum stylesheets variants views visualize voting

Recent Posts

  • Open Source Web Icons
  • Devise tutorial: adding avatars with active storage
  • SEO friendly URLs with friendly_id
  • Heroku Environment Variables
  • Enabling Gzip Compression in your rails apps

Archives

  • October 2021
  • September 2021
  • August 2021
  • July 2021

Categories

  • Activity Logging
  • Apps
  • Assets
  • Attachments
  • Audio
  • Authentication
  • Authorization
  • Deployments
  • Error Pages
  • File Uploads
  • General
  • Heroku
  • Heroku
  • Pagination
  • RubyGems
  • Search Engine Optimization
  • Search/Indexing
  • User Interface
  • Video
  • Voting
©2026 Railshero | Theme: Wordly by SuperbThemes
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept All”, you consent to the use of ALL the cookies. However, you may visit "Cookie Settings" to provide a controlled consent.
Cookie SettingsAccept All
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT