The will_paginate gem has long since been the most popular gem used when it comes to pagination. However it is not the only gem out there. Kaminari is another very popular pagination gem. In this article we will show you how to use it in your application.
Setup Rails Application
To begin using the kaminari gem, we must first add it to our Gemfile. Open up your Gemfile and add in the line listed below.
Now run a bundle install to install the gem.
Next, create a model called Post. The post model in this example will represent a blog post. Run the commands below to create the post model:
We need some seed data. Open your
db/seeds.rb file and modify it so that it looks like the code listed below:
1 2 3 4 5
Now run a rake db:seed to add the seed data:
We need to add a controller called Posts that we will use to render the posts. Run the command below to create this controller now:
Let’s modify our routes file to set a site root. Open up your config/routes.rb file and modify it so that it looks like the code listed below.
1 2 3
Open up your Posts controller and modify it so that it looks like the code listed below.
1 2 3 4 5
As you can see, the first bit of our pagination code is here. The
page method tells kaminari what page we are on. The
per method tells kaminari how many items we wish to have per page.
Let’s open up the
index view for our
Posts controller and modify it so that it looks like the code listed below:
1 2 3 4 5 6 7 8 9 10 11
paginate helper is the line actually responsible for rendering the pagination links. The
page_entries_info helper displays a line similar to Displaying posts 1 - 10 of 100 in total. This can be extremely helpful in letting the user know how many items there are in the list.
You can also specify global defaults for Kaminari. First, run the following command to generate an initializer file for Kaminari:
If you open up the
config/initializers/kaminari_config.rb file you’ll see something similar to the following:
1 2 3 4 5 6 7 8 9 10
A description of these options can be found below:
|config.default_per_page||Specifies the default amount of items to display per page.|
|config.max_per_page||The maximum amount of items to display per page.|
|config.window||Specifies the inner window size. The inner window is the number of items in the middle of the pagination block. e.g. « First ‹ Prev … 1 2 3 4 … Next › Last », in the previous example, 1 2 3 4 would be considered the inner window.|
|config.outer_window||Specifies how many items to display in the outer window. For example: 1 2 3 4 … 8 9 10 11, numbers visible on the outside are the outer window.|
|config.left||Specifies how many items should be displayed in the left outer window.|
|config.right||Specifies how many items should be displayed in the right outer window.|
|config.page_method_name||Changes the page method name from page to whatever you want. Can be used to avoid conflict with other gems that use page.|
|config.param_name||The default parameter to use when looking for the page number.|
Kaminari can be configured via I18N. The default I18N configuration file looks like the code listed below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
To override it, simply modify your locale files under
So far so good, That’s it!!! See ya!!! :)