Kaminari --Added pagination function of Rails

What is pagination?

** pagination ・ ・ ・ Pagination, numbers indicating pages ** Quoted from weblio

As with pagination, it is OK to recognize that you have a page.

For example, let's say you want to display all posts in a list view with the posting app that many people use. Without pagination, all posts would be displayed on one page. There is no problem if the number of posts is about 10, but if you display all the posts at once with an app that is used by multiple people, 100, 1000 will be displayed on one page, which makes it very difficult to see. It is in a chaotic state. .. ..

However, with Pagenation, even if you have a large number of posts, the number of posts displayed on one page is fixed. Therefore, the post can be displayed over several pages without becoming chaotic.

kaminari You can easily implement pagination functionality with one of the Rails gems. This time, we will implement the pagination function in the bulletin board list display function.

Although it is in English, you can check the specifications on Github, so please refer to this as well. https://github.com/kaminari/kaminari

Introduced kaminari

Installation method

First listed in the Gemfile to install kaminari

Gemfile


gem 'kaminari'

And the usual bundle install

$ bundle install

Edit controller

Since it is a list display function, we will edit the index action. First, check the state before editing.

app/controllers/boards_controller.rb(Before editing)


  def index
    @boards = Board.all.includes(:user).order(created_at: :desc)
  end

Board is a bulletin board model. Browse and get all posts stored in the DB with Board.all. Then, with includes (: user), the user ID (user_id) of the Board model is referenced at the same time when the Board model is referenced. Gets posts in chronological order with the date and time created by order (created_at:: desc).

I'm wondering why you do that, but I've introduced it in another article of my own, so I hope you'll take a look there. N + 1 problem-explained in Rails!

Click here after editing the above file

app/controllers/boards_controller.rb(After editing)


  def index
    @boards = Board.all.includes(:user).order(created_at: :desc).page(params[:page])
  end

page (params [: page]) came out. Here are the methods you can use by installing kaminari. In fact, with the introduction of kaminari, the parameter key has increased by one.

params
=> <ActionController::Parameters {"page"=>"2",,,,

So that's it. Apparently, the page key is used to determine which page. By increasing this key, it is possible to get the page number of the post with page (params [: page]).

Add paginate to view

Now that you can get posts for each page, let's implement it to move pages. It looks like this:

image.png

Now add the following statement to the view.

erb:app/views/boards/index.html.erb


<div class='mb-3'>
  <%= paginate @boards %>
</div>

paginate allows you to move to the next or previous page.

Perhaps it looks different from the image? I think that the display will be the same with the following command, so try typing in the terminal.

$ bin/rails g kaminari:views bootstrap4

This completes the implementation of the main pagenations. It's that easy!

Specify the number to be displayed on the page

Actually, the number of posts displayed on each page is 25 at the moment. Twenty-five are fixed by default. You can see the default settings for kaminari by running the following command.

$ rails g kaminari:config

Looking at the created file

config/initializers/kaminari_config.rb


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
  # config.max_pages = nil
  # config.params_on_first_page = false
end

I think that various settings are displayed. This time I will change the default number of pages from 25 to 10.

config/initializers/kaminari_config.rb


Kaminari.configure do |config|
  #Change from 25 to 10
  # config.default_per_page = 10
  # 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

The number of posts displayed per page is now 10.

However, as I said earlier, this file sets the default value for kaminari. In other words, the settings are common to all **. There is no problem if you only page to the post list function. However, if you implement a comment function for a post and pagination for that comment, that comment will display 10 comments per page.

There are ways to avoid this. Set the number of displays per page individually for each model.

If you want to display 10 posts per page, add that setting directly to the model.

app/models/board.rb


class Board < ApplicationRecord
  # specify default per_page value per each board
  paginates_per 20

You can override the default value with paginate_per. If you want to implement pagination in the comment function, you can write paginate_per in the same way for the comment model.

Recommended Posts

Kaminari --Added pagination function of Rails
[Rails 6] Pagination function implementation (kaminari)
Pagination function (kaminari)
Create pagination function with Rails Kaminari
About rails kaminari pagination
Implementation of pagination function
[Rails 6] Implementation of search function
[Rails] Implementation of category function
[Rails] Implementation of tutorial function
[Rails] Implementation of like function
[Rails] Implementation of CSV import function
[Rails] Asynchronous implementation of like 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
First pagination feature added in rails
[Rails] Addition of Ruby On Rails comment function
Rails Addition of easy and easy login function
Rails [For beginners] Implementation of comment function
[Rails 6] Implementation of SNS (Twitter) sharing function
[Vue.js] Implementation of menu function Implementation version rails6
[Ruby on rails] Implementation of like function
[Vue.js] Implementation of menu function Vue.js introduction rails6
[Rails 6] Ranking function
Implement Rails pagination
Strict_loading function to suppress the occurrence of N + 1 problem added from rails 6.1
[Rails] Category function
Rails follow function
[Rails] Notification function
[Rails] Implementation of search function using gem's ransack
Implementation of Ruby on Rails login function (Session)
[Rails 6] Implementation of inquiry function using Action Mailer
[Rails] Implementation of retweet function in SNS application
[Note] Summary of rails login function using devise ①
[Rails] Ranking and pagination in order of likes
Asynchronously output "html element of pagination" with kaminari
Ruby on Rails <2021> Implementation of simple login function (form_with)
[Rails] Implementation of drag and drop function (with effect)
[Rails] Test of star evaluation function using Raty [Rspec]
[Rails] Introduction of PAY.JP
[Rails] Implement search function
Implementation of search function
[Rails] Implemented hashtag function
[Ruby on Rails] Implementation of tagging function/tag filtering function
[Rails] Implementation of multi-layer category function using ancestry "Preparation"
[rails] tag ranking function
Rails Tutorial/Significance of Indexing
[Rails] Implementation of multi-layer category function using ancestry "seed"
Rails search function implementation
[Rails] Implementation of SNS authentication (Twitter, Facebook, Google) function
Rails tutorial (6th edition) Background operation of login function
[Rails] Implementation of multi-layer category function using ancestry "Editing form"
[Rails] Implementation of multi-layer category function using ancestry "Creation form"
A note about the seed function of Ruby on Rails
Rails sorting function implementation (displayed in order of number of like)
[Rails] Implementation of tagging function using intermediate table (without Gem)
Rails tutorial (6th edition) Background operation of password reset function
Addition of guest login function
Implement application function in Rails
Rails implementation of ajax removal