From their GitHub page: "A Scope & Engine based, clean, powerful, customizable and sophisticated paginator for modern web app frameworks and ORMs"
- Easy to use
- No configuration required
- Don't have to define anything in your models or helpers.
Add this line to your Gemfile
gem 'kaminari'
Then don't forget to run bundle
in terminal!
In your controller, call the .page
method on your model.
# creatures_controller.rb
class CreaturesController < ApplicationController
def index
@creatures = Creature.page(params[:page])
end
By default, kaminari loads 25 records per page, but you could change that by adding .per(number)
to the previous line of code.
# creatures_controller.rb
class CreaturesController < ApplicationController
def index
@creatures = Creature.page(params[:page]).per(5)
end
Then enter this paginate
helper wherever you want the paginated pages to show in your view.
# app/views/creatures/index.html.erb
<%= paginate @creatures %>
To fetch the 3rd page of users use the .page
query method
Creature.page(3)
- Pagination starts on page 1 not 0.
For more, GitHub Query Basics