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

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1
    • 0
    • 633
    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:

    gem 'kaminari'


    Step 2: Run the bundle

    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


    per scope: Used for defining the per page records

    Product.page(3).per(10)
    #=> 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:

    class Product < ActiveRecord::Base
      paginates_per 50
      max_paginates_per 100
    end


    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
    

    These configurable methods can be set by using a generator:

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

    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
    

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

    <%= paginate @products %>

    This will look like this:

    Kaminari Gem

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: