Pagination using Kaminari in Rails 5


Step 1

Add the Kaminari gem to the Gemfile:

gem 'kaminari', :git => "git://", :branch => 'master'

Run bundle:


Step 2

Add the number of tasks to be displayed per page in task model:

paginates_per  5

Step 3

The search method now needs to use Kaminari gem provided page method like this:

def, page)
  if term
    where('name LIKE ?', "%#{term}%").order('id DESC').page(current_page)
    # note: default is all, just sorted
    order('id DESC').page(current_page) 

Step 4

Add the paginate view helper in tasks/index.html.erb:

<%= paginate @tasks %>

Step 5

You can customize the number of links that is displayed.

<%= paginate @tasks, :left => 1, :right => 3 %>

Step 6

Another option to customize the pagination:

<%= paginate @tasks, :outer_window => 3 %>

Step 7

You can use custom themes. Let's install google theme.

rails g kaminari:views google

The pagination now has Google look and feel. There are other themes available such as bootstrap, github etc, checkout the Kaminari home page for more details.


In this article, you learned how to paginate using Kaminari Gem in Rails 5.

Related Articles

Ace the Technical Interview

  • Easily find the gaps in your knowledge
  • Get customized lessons based on where you are
  • Take consistent action everyday
  • Builtin accountability to keep you on track
  • You will solve bigger problems over time
  • Get the job of your dreams

Take the 30 Day Coding Skills Challenge

Gain confidence to attend the interview

No spam ever. Unsubscribe anytime.