Pagination function (kaminari)

Introduction

I am a beginner who is currently working on a portfolio. It is a memorandum because I introduced kaminari and implemented the pagination function.

1. Install gem

Gemfile


gem 'kaminari'

Terminal


% bundle install

2. Defined in the controller

events_controller.rb


def index
  @events = Event.page(params[:page]).per(10)
end

A method called __page and per is a method defined in kaminari. You can specify how many records should be displayed in the argument of the per method to increase the page. __

3. Edit the view file

ruby:events/index.html.erb


<%= paginate @events %>

__ Described in the view file you want to display. __

__that's all! __

Change the appearance of pagenation

Pagination displayed by kaminari cannot be css in the view. So the following method.

Terminal


% rails g kaminari:views default

Added kaminari to the __app/views folder. __ Since the html of the pagination part is described in the __ folder, change it there. __

When applying the css framework design

If you are using a css framework such as bootstrap, the display and design may be corrupted. Specify the default part in the name of each framework. If you are using bootstrap4, use the following command.

Terminal


% rails g kaminari:views bootstrap4

__ If you run this command and create a view file, it will be optimized for each framework style without any editing. __

Japaneseize kaminari

Since it is written in English by default, to change it to Japanese notation Create a file called kaminari_ja.yml in the config/locales folder and write the following code.

config/locales/kaminari_ja.yml


ja:
  views:
    pagination:
      first: "&laquo;the first"
      last: "last&raquo;"
      previous: "&lsaquo;Before"
      next: "Next&rsaquo;"
      truncate: "..."

reference

https://pikawaka.com/rails/kaminari

Recommended Posts

Pagination function (kaminari)
[Rails 6] Pagination function implementation (kaminari)
Kaminari --Added pagination function of Rails
Create pagination function with Rails Kaminari
About rails kaminari pagination
Implementation of pagination function
Login function
JavaScript function
SpringMVC-Interceptor function
How to create pagination for a "kaminari" array
Asynchronously output "html element of pagination" with kaminari