Blog

will_paginate vs. Kaminari

Icône flèche bleue vers la gauche
Back to blog
will_paginate vs. Kaminari

will_paginate vs. Kaminari

April 2, 2012

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

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

and in your view:

index.html.erb

<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

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

def index @books
Book.page(params[:page]).per(10)
end

and in your view:

index.html.erb

<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

<%= 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

if defined?(WillPaginate)
module WillPaginate
module ActiveRecord
module RelationMethods
alias_method :per, :per_pag
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

Ready to build your software product? Contact us!