Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Kaminari Gem

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1
    • 0
    • 636
    Comment on it

    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:

    1. gem 'kaminari'


    Step 2: Run the bundle

    1. 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

    1. Product.page(3)
    2. #=> 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

    1. Product.page(3).per(10)
    2. #=> It will set the per page records, Thus it will return the record from 21st to 30th based upon the order

    You can also configure paginates_per and max_paginates_per values on model basis by defining them in the model like this:

    1. class Product < ActiveRecord::Base
    2. paginates_per 50
    3. max_paginates_per 100
    4. end


    There are so many other options that can be configured that are:

    1. default_per_page # 25 by default
    2. max_per_page # nil by default
    3. max_pages # nil by default
    4. window # 4 by default
    5. outer_window # 0 by default
    6. left # 0 by default
    7. right # 0 by default
    8. page_method_name # :page by default
    9. param_name # :page by default
    10. params_on_first_page # false by default
    11.  

    These configurable methods can be set by using a generator:

    1. rails g kaminari:config
    2.  
    3. ##create config/initializers/kaminari_config.rb

    It will create the kaminari_config file where you can set the above mentioned things

    1. Kaminari.configure do |config|
    2. # config.default_per_page = 25
    3. # config.max_per_page = nil
    4. # config.window = 4
    5. # config.outer_window = 0
    6. # config.left = 0
    7. # config.right = 0
    8. # config.page_method_name = :page
    9. # config.param_name = :page
    10. end
    11.  

    Now at last you need to add the view helper to show the page numbers in the view:

    1. <%= paginate @products %>

    This will look like this:

    Kaminari Gem

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: