will_paginate is a pagination library that integrates with Ruby on Rails, Sinatra, Merb, DataMapper and Sequel.
Installation:
## Gemfile for Rails 3+, Sinatra, and Merb
gem 'will_paginate', '~> 3.1.0'
See installation instructions on the wiki for more info.
## 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 %>
And that's it! You're done. You just need to add some CSS styles to make those pagination links prettier.
You can customize the default "per_page" value:
# for the Post model
class Post
self.per_page = 10
end
# 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
Post.page(params[:page]).order('created_at DESC')
See the wiki for more documentation. Ask on the group if you have usage questions. Report bugs on GitHub.
Happy paginating.