<img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?id=706355809571598&amp;ev=PageView&amp;noscript=1"> will_paginate vs. Kaminari

In order to start this comparison, let’s just explain the way of working of both gem.

Assume we have a class of objects named Book.

In order to paginate your list of books, will_paginate needs you to put:

In your controller:

books_controller.rb
1
2
3
def index
 @books = Book.paginate(:page => params[:page], :per_page => 10)
end

and in your view:

index.html.erb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<table>
 <thead>
 <tr>
 <th>Title</th>
 <th>Price</th>
 </tr>
 </thead>
 <tbody>
 <% @books.each do |book| %>
 <tr>
 <td><%= book.name %></td>
 <td><%= book.price %></td>
 </tr>
 <% end %>
 </tbody>
</table>
<%= will_paginate @books %>

TIP: If you have to paginate an array (instead of an activerecord collection), just add this line in one of your initializer:

will_paginate_array.rb
1
require 'will_paginate/array'

You obtain directly the pagination you want. One drawback, additionally with this small disadvantage of not handling arrays natively, is that you can’t personalize the way the HTML and links are generated by will_paginate (for instance, if you need to add some parameters to links, or if you want to use div instead of span, whatever).

Let’s now see how Kaminari works!

In your controller:

books_controller.rb
1
2
3
def index
 @books = Book.page(params[:page]).per(10)
end

and in your view:

index.html.erb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<table>
 <thead>
 <tr>
 <th>Title</th>
 <th>Price</th>
 </tr>
 </thead>
 <tbody>
 <% @books.each do |book| %>
 <tr>
 <td><%= book.name %></td>
 <td><%= book.price %></td>
 </tr>
 <% end %>
 </tbody>
</table>
<%= paginate @books %>

As you see, both systems are similar. But Kaminari improves that by allowing you to personalize your pagination. You can generate default view templates (via the following command rails g kaminari:views default) and even create themes if you need several distinct presentations (Go to the newly created kaminari folder in your views, create a new folder for your theme and copy / paste default views previously generated in this folder and modify them as you desire). Then, you simply have to call

1
<%= paginate @books, :theme => 'my_theme' %>. 

Both gems give straightly a good pagination system to your application, you can even use several ones on a same page, but our love can’t go to one of them, due to their pro and con’s, Kaminari is really amazing with his additional personalization features, nevertheless, it doesn’t handle arrays at all (even if some tricks could be used in that purpose). Will paginate, in the other hand, can handle a lot more situations (arrays, activeRecord associations, …).

Furthermore, if you have to use both gems in your app (Yes, it could happen ;) ). Just add this in one of your initializer:

will_paginate_kaminari.rb
1
2
3
4
5
6
7
8
9
10
if defined?(WillPaginate)
 module WillPaginate
 module ActiveRecord
 module RelationMethods
 alias_method :per, :per_page
 alias_method :num_pages, :total_pages
 end
 end
 end
end

For further information about these two gems:

will_paginate: https://github.com/mislav/will_paginate

Kaminari: https://github.com/amatsuda/kaminari