[Ruby] Implementation of pagination function

less than 1 minute read


・ Ruby 2.5.7
・ Rails

I want to display only 10 items per page using the pagination function

The image looks like this

First install gem

To implement the pagination function, use the gem “kaminari”.
First, describe kaminari in the gemfile.
After adding to the Gemfile, run bundle install in the terminal to install kaminari.

Then execute the following command

Execute the following to create the kaminari configuration file. If you move the numbers in this file, you can set how many items can be displayed per page.
スクリーンショット 2020-08-08 22 31 08
You can modify the design of kaminari that is generated by executing the following.
For details, this article was easy to understand.
スクリーンショット 2020-08-08 22 37 13

Changed view and controller for pagination

To use kaminari, add the following to the view file you want to use.
スクリーンショット 2020-08-08 22 41 41
The controller also got it with all, but modify it as follows.
If you specify reverse_order, it will be displayed in the newest order.
スクリーンショット 2020-08-08 22 52 35

Enter how many items you want to display in kaminari_config.rb in the config / initializers folder and you’re done! !!

スクリーンショット 2020-08-08 22 55 08