[Rails 6] Pagination function implementation (kaminari)

Introduction

We are creating an article search / browsing application. This time, the search result list display will be huge on one page, so I would like to implement a pagination function and limit the number displayed on one page to achieve a neat appearance.

environment

Ruby on Rails '6.0.0' Ruby '2.6.5'

Premise

The user searches for articles, and the article list is displayed and can be viewed.

① Introduction of Gem

Introduce gem "** kaminari **" that can easily implement pagination function.

Gemfile


gem 'kaminari'

Terminal


% bundle install

② Define page nation on the controller

app/controllers/articles_controller.rb


  def index
    @articles = Article.page(params[:page]).per(8).order('created_at DESC')
  end

The methods "page" and "per" are defined in kaminari. It specifies how many articles (records) should be displayed in the argument of the per method to increase the number of pages.

③ Edit view file

ruby:app/views/articles/index.html.erb


(abridgement)
 <%= paginate @articles %>

All you have to do is write the above in the view file you want to display! !! !!

Bonus (customization of pagenation)

The pagination function can be introduced in the above three steps, but the view file is determined by default. Describes how to fix it.

Terminal


% rails g kaminari:views default (①)

              or

% rails g kaminari:views bootstrap4 (②)

With the command ①, kaminari will be added under app/views, so you can make changes there. You can apply a CSS framework such as bootstrap with the command (2).

Also, since it is written in English by default, in order to change it to Japanese notation, you can apply it by creating a file called kaminari_ja.yml under config/locales and writing as follows.

config/locales/kaminari_ja.yml


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

in conclusion

https://pikawaka.com/rails/kaminari I referred to the above article. Thank you very much! !!

Recommended Posts

[Rails 6] Pagination function implementation (kaminari)
Pagination function (kaminari)
Kaminari --Added pagination function of Rails
Create pagination function with Rails Kaminari
About rails kaminari pagination
Rails search function implementation
Implementation of pagination function
Rails fuzzy search function implementation
[Rails] Implementation of category function
Login function implementation with rails
[Rails] Implementation of tutorial function
[Rails] Implementation of like function
[Rails] Implementation of CSV import function
[Rails] Implementation of image preview function
[Rails] About implementation of like function
[Rails] Implementation of user withdrawal function
[Rails] Implementation of CSV export function
[Ruby on Rails] Comment function implementation
[Rails 6] Like function (synchronous → asynchronous) implementation
[Rails] Comment function implementation procedure memo
DM function implementation
[Rails 6] Ranking function
[Ruby on Rails] Follow function implementation: Bidirectional
[Rails] Category function
Rails [For beginners] Implementation of comment function
Rails follow function
Rails Basic CRUD function implementation procedure scaffold
[Rails 6] Implementation of SNS (Twitter) sharing function
[Ruby on rails] Implementation of like function
[Vue.js] Implementation of menu function Vue.js introduction rails6
[Rails] Notification function
Implementation of Ruby on Rails login function (Session)
[Rails 6] Implementation of inquiry function using Action Mailer
[Rails] Implementation of image enlargement function using lightbox2
[Rails] Implementation of retweet function in SNS application
Ruby on Rails Email automatic sending function implementation
Rails hashtag search implementation
Rails6 countdown timer implementation
[rails] gem'payjp' implementation procedure
Comment function (Ajax) implementation
Rails Action Text implementation
Follow function (Ajax) implementation
[Rails] Implement search function
Implementation of search function
[Rails] Implemented hashtag function
Image preview function implementation
Search function [copype implementation]
Pagination implementation with gem'kaminari'
Ruby on Rails <2021> Implementation of simple login function (form_with)
Implementation of Ruby on Rails login function (devise edition)
Rails CRUD function implementation ② (edited and detailed this time)
[Ruby on Rails] Implementation of tagging function/tag filtering function
[Rails] Implementation of multi-layer category function using ancestry "Preparation"
[Rails] Implementation of multi-layer category function using ancestry "seed"
[Rails] Implementation of SNS authentication (Twitter, Facebook, Google) function
Rails CRUD function implementation ① (new addition, deletion this time)
Implement application function in Rails
[Rails] Implementation of multi-layer category function using ancestry "Editing form"
[Rails] Implement User search function
Introduced graph function with rails
Search function using [rails] ransack