[Ruby on Rails] How to use kaminari

What is kaminari?

It is one of the ruby ​​gems and is for making page nations.

installation of kaminari

Add the following to Gemfile.

gem 'kaminari'

Install in the terminal.

$ bundle install

Added pagenation code to controller


class PostsController < ApplicationController

def index
  @posts = Post.all
  @posts = Post.page(params[:page]).per(15)
end

end

Set how many to display per page in the part of page (params [: page]). per (15). Pagination is displayed when the number displayed is greater than the set number.

Add pagination code to view file

Add it where you want the pagenation to appear in the view.

    <%= paginate @posts %>

Japaneseize kaminari

The default display of kaminari is English. For Japanese localization, it is easier to manage by creating a file called kaminari_ja.yml in config/locales. Add the following to kaminari_ja.yml.

ja:
  views:
    pagination:
      first: "&laquo;the first"
      last: "last&raquo;"
      previous: "&lsaquo;Before"
      next: "Next&rsaquo;"
      truncate: "..."
  helpers:
    page_entries_info:
      one_page:
        display_entries:
          zero: ""
          one: "<strong>1-1</strong>/1 case"
          other: "<strong>1-%{count}</strong>/%{count}In case"
      more_pages:
        display_entries: "<strong>%{first}-%{last}</strong>/%{total}In case"

Recommended Posts

[Ruby on Rails] How to use kaminari
How to use Ruby on Rails
[Ruby on Rails] How to use CarrierWave
[Ruby on Rails] How to use redirect_to
[Rails] How to use "kaminari"
[Ruby on Rails] How to use session method
[Ruby on Rails] How to display error messages
How to add / remove Ruby on Rails columns
How to use Ruby return
[Rails] How to use enum
How to use rails join
[Ruby on Rails] "|| =" ← Summary of how to use this assignment operator
[Rails] How to use authenticate_user!
[Ruby on Rails] How to install Bootstrap in Rails
[Rails] How to use Scope
[Ruby on Rails] How to write enum in Japanese
[Ruby On Rails] How to reset DB in Heroku
(Ruby on Rails6) How to create models and tables
[Rails] How to use gem "devise"
How to deploy jQuery on Rails
[Rails] How to use devise (Note)
[Rails] How to use flash messages
How to deploy Bootstrap on Rails
[Ruby] How to use any? Method
[Rails] How to use Active Storage
[Introduction to Rails] How to use render
How to use Ruby inject method
How to display a graph in Ruby on Rails (LazyHighChart)
A memorandum on how to use Eclipse
How to use Apache Derby on Eclipse
How to use custom helpers in rails
[Ruby On Rails] How to use simple_format to display the entered text with line breaks
[Rails] How to use rails console with docker
[Rails] How to use ActiveRecord :: Bitemporal (BiTemporalDataModel)
[Rails] How to use the map method
Ruby length, size, count How to use
How to use MySQL in Rails tutorial
Deploy to Heroku [Ruby on Rails] Beginner
[Ruby] How to use slice for beginners
Preparing to introduce jQuery to Ruby on Rails
[Note] How to use Rails 6 Devise + cancancan
[Rails] How to use video_tag to display videos
[Ruby on Rails] Button to return to top
Ruby: CSV :: How to use Table Note
[Rails] How to use helper method, confimartion
How to use credentials.yml.enc introduced in Rails 5.2
Explanation of Ruby on rails for beginners ④ ~ Naming convention and how to use form_Tag ~
[Ruby on Rails] Use the resources method to automatically create routes.
[Ruby on Rails] How to avoid creating unnecessary routes for devise
How to use Map
How to write Rails
How to use rbenv
How to use letter_opener_web
How to use with_option
How to use fields_for
How to use java.util.logging
How to use map
How to use collection_select
How to use Twitter4J
How to use active_hash! !!
How to use MapStruct