using kaminari or will_paginate

A Scope & Engine based, clean, powerful, customizable and sophisticated paginator for Rails 3

Does not globally pollute Array, Hash, Object or AR::Base.
Easy to use

Just bundle the gem, then your models are ready to be paginated. No configuration required. Don’t have to define anything in your models or helpers.
Simple scope-based API

Everything is method chainable with less “Hasheritis”. You know, that’s the Rails 3 way. No special collection class or anything for the paginated values, instead using a general AR::Relation instance. So, of course you can chain any other conditions before or after the paginator scope.
Customizable engine-based I18n-aware helper

As the whole pagination helper is basically just a collection of links and non-links, Kaminari renders each of them through its own partial template inside the Engine. So, you can easily modify their behaviour, style or whatever by overriding partial templates.
ORM & template engine agnostic

Kaminari supports multiple ORMs (ActiveRecord, Mongoid, MongoMapper) and multiple template engines (ERB, Haml).

The pagination helper outputs the HTML5 tag by default. Plus, the helper supports Rails 3 unobtrusive Ajax.

Supported versions

  • Ruby 1.8.7, 1.9.2, 1.9.3 (trunk)
  • Rails 3.0.x, 3.1 (edge)
  • Haml 3
  • Mongoid 2
  • MongoMapper 0.9
  • DataMapper 1.1.0


Put this line in your Gemfile:

gem 'kaminari'

next open your terminal and run bundle install

Configuring default per_page value for each model

You can specify default per_page value per each model using the following declarative DSL.

class User < ActiveRecord::Base
  paginates_per 50


the page parameter is in params[:page]
Typically, your controller code will look like this:

@users = User.order(:name).page params[:page]


the same old helper method
Just call the paginate helper:

<%= paginate @users %>


will_paginate is a pagination library that integrates with Ruby on Rails, Sinatra, Merb, DataMapper and Sequel.

## Gemfile for Rails 3, Sinatra, and Merb
gem 'will_paginate', '~> 3.0'

Basic will_paginate use

## perform a paginated query:
@posts = Post.paginate(:page => params[:page])

# or, use an explicit "per page" limit:
Post.paginate(:page => params[:page], :per_page => 30)

## render page links in the view:
<%= will_paginate @posts %>

You can customize the default “per_page” value:

# for the Post model
class Post
  self.per_page = 10

# set per_page globally
WillPaginate.per_page = 10

New in Active Record 3:

# paginate in Active Record now returns a Relation
Post.where(:published => true).paginate(:page => params[:page]).order('id DESC')

# the new, shorter page() method[:page]).order('created_at DESC')

About me
Interested about Ruby on Rails

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: