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.
Ruby on Rails '6.0.0' Ruby '2.6.5'
The user searches for articles, and the article list is displayed and can be viewed.
Introduce gem "** kaminari **" that can easily implement pagination function.
% bundle install
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.
(abridgement) <%= paginate @articles %>
All you have to do is write the above in the view file you want to display! !! !!
The pagination function can be introduced in the above three steps, but the view file is determined by default. Describes how to fix it.
% 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.
ja: views: pagination: first: "«the first" last: "last»" previous: "‹Before" next: "Next›" truncate: "..."
https://pikawaka.com/rails/kaminari I referred to the above article. Thank you very much! !!