Steps to execute pagination using “Kaminari” gem in Rails3

Rails3 123
Social sharing

Pagination is a technique that divides content into manageable chunks, allowing users to easily browse through a dataset. 

In this blog, we’ll dive into the process of implementing pagination using the “Kaminari” gem in a Rails 3 application. 

Kaminari is a powerful and flexible gem that simplifies the pagination process, enhancing the performance and usability of your web application.

Contents

Key Features:

  • Easy to use.
  • Customizable engine-based I18n-aware helper.
  • The pagination helper outputs the HTML5 <nav> tag by default and the helper supports Rails 3 unobtrusive Ajax.

Recommended Reading: AJAX Pagination using jQuery in Rails3

Here are the steps to implement “kaminari gem” in a Rails app.

Step#1

  • Put this code in your Gemfile:
gem 'kaminari'
  • Run “bundle install”

Step#2

  • Modify the controller as mentioned below
@blogs = Blog.order("name").page(params[:page])

Step#3

  • Now, add the paginate helper method in your listing page which is provided by Kaminari, by passing in the list we’re paginating.
<%= paginate @blogs%>

Step#4

  • When the page reloads, the pagination links will be visible. Kaminari will show 25 items per page by default, but we can easily change that by calling another scope called “per” as mentioned below.
@blogs = Blog.order("name").page(params[:page]).per(10)
  • Now it should display 10 items per page.

Step#5

  • You can configure the below mentioned default values of kaminari by running the command
rails g kaminari:config

It’ll generate a file “kaminari_config.rb” in your config/initializers folder with the following code snippet as commented.

default_per_page   # 25 by default
max_per_page       # nil by default
window             # 4 by default
outer_window       # 0 by default
left               # 0 by default
right              # 0 by default
page_method_name   # :page by default
param_name         # :page by default
  • Next, you can change the values according to your requirement.
  • If you change your view page like:
<%= paginate @blogs, :window => 2 %> 
  • The output will be:
« First ‹ Prev 1 2 3 4 5 … Next › Last »
  • There are some other custom attributes you can use in view page like:
<%= paginate @users, :outer_window => 3 %>
<%= paginate @users, :left => 1, :right => 3 %>
<%= paginate @users, :params => {:controller => 'foo', :action => 'bar'} %>
<%= paginate @users, :remote => true %> 

Kaminari also includes a handy template generator. You can override them by running following command.

rails g kaminari:views default

It’ll generate a “Kaminari” folder in app/views with dependent files inside it.

I hope you liked it. If you want you can contact our experienced ruby on rails developer for your webs and mobile application.
Please leave your comment about this post on the comment section below.

Your recently viewed posts:

    Contact Us

    We’d love to help & work with you




    When do you want to start ?


    Enter your email address to stay up to date with the latest news.
    Holler Box

    Orange Exit pop up

    Subscribe for the latest
    trends in web and
    mobile app development
    Holler Box

    Exit pop up

    Sad to see you leaving early...

    From "Aha" to "Oh shit" we are sharing everything on our journey.
    Enter your email address to stay up to date with the latest news.
    Holler Box