over 8 years ago
Hi Friends,
Pagination helps us distributing a large amount of data in batches and show them in the views. In rails there are so many gems available for pagination like will paginate, kaminari. Today I am going to tell you how we can integrate Kaminari gem in our rails application.
Step 1: Add the gem to the Gemfile:
Step 2: Run the bundle
Step 3: That's it the kaminari is now integrated in your app. So as we all know the pagination depends on two things. One is page number and the second is per page records. So there are two major scopes available here:
page scope: Used for page number
Product.page(3)
#=> It will give the page number 3 of the products. By default the default per page records are 25 so the records will be served from 51st to 75h depending on the order
Product.page(3)
#=> It will give the page number 3 of the products. By default the default per page records are 25 so the records will be served from 51st to 75h depending on the order
per scope: Used for defining the per page records
You can also configure paginates_per and max_paginates_per values on model basis by defining them in the model like this:
There are so many other options that can be configured that are:
default_per_page # 25 by default
max_per_page # nil by default
max_pages # nil by default
window # 4 by default
outer_window # 0 by default
left # 0 by default
right # 0 by default
page_method_name # :page by default
param_name # :page by default
params_on_first_page # false by default
default_per_page # 25 by default
max_per_page # nil by default
max_pages # nil by default
window # 4 by default
outer_window # 0 by default
left # 0 by default
right # 0 by default
page_method_name # :page by default
param_name # :page by default
params_on_first_page # false by default
These configurable methods can be set by using a generator:
It will create the kaminari_config file where you can set the above mentioned things
Kaminari.configure do |config|
# config.default_per_page = 25
# config.max_per_page = nil
# config.window = 4
# config.outer_window = 0
# config.left = 0
# config.right = 0
# config.page_method_name = :page
# config.param_name = :page
end
Kaminari.configure do |config|
# config.default_per_page = 25
# config.max_per_page = nil
# config.window = 4
# config.outer_window = 0
# config.left = 0
# config.right = 0
# config.page_method_name = :page
# config.param_name = :page
end
Now at last you need to add the view helper to show the page numbers in the view:
This will look like this:
0 Comment(s)