[Rails] How to use "kaminari"

Introduction

I've implemented "kaminari" so I'll write it down so I don't forget it. : zap:

「kaminari」

It is one of ruby ​​gems for implementing pagination.

When displaying a list page of something, if there are many, the page is divided by numbers so that it is easy to see, and a button that can jump to the first or last page can be implemented.

Implementation

Installation

First, install the gem. Since it will be written in Japanese, install "i18n" together.

I've learned "i18n" before, so I'll post it here.

[Rails] How to write in Japanese

Gemfile



gem 'kaminari'
gem 'rails-i18n'

Install!


bundle install

controller

controller.rb



def index
    @advises = Advise.page(params[:page])
  end

View file

ruby:view.html.erb



 <div class="advise-lists">
   <% @advises.each do |advise| %>
     <ul class="advise-text">
       <li><%= link_to advise.title, advise_path(advise), class: "advise2_link" %></li>
     </ul>
   <% end %>     <!--↑ List display part-->            
 </div>         <!--↓ Pagination implementation part-->      

 <%= paginate @advises %>

    

Create a "kaminari" configuration file


rails g kaminari:config

A "kaminari_config" file is created. You can change it to your liking.

Here, "default_per_page" is intentionally set as small as 3 lines per page.

config/initializers/kaminari_config.rb



# frozen_string_literal: true

Kaminari.configure do |config|
 config.default_per_page = 3
  # 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
  # config.max_pages = nil
  # config.params_on_first_page = false
end

There are many items here we have Freelance LIFE !: Implement pagenation with gem in [Rails]!

TECH SCORE BLOG: Rails library introduction: "kaminari" for paging

It is explained as follows.

default_per_page The default number of items displayed per page (default is 25).

max_per_page Maximum number of views per page (default is nil, or unlimited).

window Specifies how many pages of links to the left and right of the displayed page are displayed (default is 4). The image above is the default of 4, showing page 11. Four pages of links are generated on each of the 11 left and right sides.

outer_window Specifies how many pages of links are displayed from the first page and the last page (default is 0). If left or right is specified, those values ​​take precedence.

left Specifies how many pages of links are displayed from the first page (default is 0). The above image is when 3 is specified.

right Specifies how many pages of links are displayed from the last page (default is 0). The above image is when 2 is specified.

page_method_name The name of the scope that specifies the page number to be added to the model (default is page).

param_name The name of the request parameter used to pass the page number (default is page).

Quote: Rails Library Introduction: "kaminari" for paging

Locale setting

Set the load path for the locale file. Set the default locale to "Japanese (ja)".

config/application.rb



config.i18n.load_path +=
      Dir[Rails.root.join("config", "locales", "**", "*.{ry,yml}").to_s]
    config.i18n.default_locale = :ja

Create a view directory and create files under it.

yml:locales/views/pagenate.ja.yml



ja:
  views:
    pagination:
      first: "lead"
      last: "last"
      previous: "Forward"
      next: "next"
      truncate: "..."

Then, the English notation will be displayed instead of the Japanese notation, and page nation will be implemented.

スクリーンショット 2021-01-15 23.30.18.png

If you want to customize further


rails g kaminari:views default

When you enter These are created in the view.

スクリーンショット 2021-01-15 23.35.38.png

You can change the paged view by editing these files. (_gap.html.erb is the "..." part where the page is omitted, _page.html.erb is the page number part, _paginator.html.erb is the whole configuration definition)

Summary

It's not that difficult to put on, but I'd like to learn a little more about how to customize it. I would like to do the decoration and customization part again soon.             :runner::dash:  :runner: :runner::runner::dash:

reference

kikawaka: [Rails] Let's master how to use kaminari!

To check the setting items ...

RAILS GUIDES:Configuring Rails Applications

Reference for decoration

Potechiru: [Rails] How to implement pagination using kaminari

Recommended Posts

[Rails] How to use "kaminari"
[Rails] How to use enum
How to use rails join
[Rails] How to use authenticate_user!
[Rails] How to use Scope
[Rails] How to use gem "devise"
[Rails] How to use devise (Note)
[Rails] How to use flash messages
How to use Ruby on Rails
[Rails] How to use Active Storage
[Introduction to Rails] How to use render
How to use Map
How to use custom helpers in rails
How to write Rails
How to use rbenv
[Ruby on Rails] How to use CarrierWave
How to use letter_opener_web
How to use with_option
How to use fields_for
How to use java.util.logging
[Rails] How to use rails console with docker
How to use map
[Rails] How to use ActiveRecord :: Bitemporal (BiTemporalDataModel)
[Rails] How to use the map method
How to use collection_select
How to use Twitter4J
How to use active_hash! !!
How to use MapStruct
How to use MySQL in Rails tutorial
How to use hidden_field_tag
How to use TreeSet
[How to use label]
How to use identity
How to use hashes
How to use JUnit 5
[Ruby on Rails] How to use redirect_to
[Note] How to use Rails 6 Devise + cancancan
How to use Dozer.mapper
How to use Gradle
[Rails] How to use video_tag to display videos
[Rails] How to use helper method, confimartion
How to use org.immutables
How to use java.util.stream.Collector
How to use VisualVM
How to use credentials.yml.enc introduced in Rails 5.2
How to use Map
[Rails] How to use select boxes in Ransack
How to use rails g scaffold, functions, precautions
How to use JQuery in js.erb of Rails6
[Rails] How to use Gem'rails-i18n' for Japanese support
[Ruby on Rails] How to use session method
[Rails] How to use PostgreSQL in Vagrant environment
[Java] How to use Map
How to use Chain API
How to use java Optional
How to use JUnit (beginner)
How to use Ruby return
How to read rails routes
How to use @Builder (Lombok)
[Swift] How to use UserDefaults
How to use java class