I've implemented "kaminari" so I'll write it down so I don't forget it. : zap:
「kaminari」
It is one of ruby gems for implementing pagination.
When displaying a list page of something, if there are many, the page is divided by numbers so that it is easy to see, and a button that can jump to the first or last page can be implemented.
First, install the gem. Since it will be written in Japanese, install "i18n" together.
I've learned "i18n" before, so I'll post it here.
[Rails] How to write in Japanese
Gemfile
gem 'kaminari'
gem 'rails-i18n'
Install!
bundle install
controller.rb
def index
@advises = Advise.page(params[:page])
end
ruby:view.html.erb
<div class="advise-lists">
<% @advises.each do |advise| %>
<ul class="advise-text">
<li><%= link_to advise.title, advise_path(advise), class: "advise2_link" %></li>
</ul>
<% end %> <!--↑ List display part-->
</div> <!--↓ Pagination implementation part-->
<%= paginate @advises %>
rails g kaminari:config
A "kaminari_config" file is created. You can change it to your liking.
Here, "default_per_page" is intentionally set as small as 3 lines per page.
config/initializers/kaminari_config.rb
# frozen_string_literal: true
Kaminari.configure do |config|
config.default_per_page = 3
# 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
There are many items here we have Freelance LIFE !: Implement pagenation with gem in [Rails]!
TECH SCORE BLOG: Rails library introduction: "kaminari" for paging
It is explained as follows.
default_per_page The default number of items displayed per page (default is 25).
max_per_page Maximum number of views per page (default is nil, or unlimited).
window Specifies how many pages of links to the left and right of the displayed page are displayed (default is 4). The image above is the default of 4, showing page 11. Four pages of links are generated on each of the 11 left and right sides.
outer_window Specifies how many pages of links are displayed from the first page and the last page (default is 0). If left or right is specified, those values take precedence.
left Specifies how many pages of links are displayed from the first page (default is 0). The above image is when 3 is specified.
right Specifies how many pages of links are displayed from the last page (default is 0). The above image is when 2 is specified.
page_method_name The name of the scope that specifies the page number to be added to the model (default is page).
param_name The name of the request parameter used to pass the page number (default is page).
Quote: Rails Library Introduction: "kaminari" for paging
Set the load path for the locale file. Set the default locale to "Japanese (ja)".
config/application.rb
config.i18n.load_path +=
Dir[Rails.root.join("config", "locales", "**", "*.{ry,yml}").to_s]
config.i18n.default_locale = :ja
Create a view directory and create files under it.
yml:locales/views/pagenate.ja.yml
ja:
views:
pagination:
first: "lead"
last: "last"
previous: "Forward"
next: "next"
truncate: "..."
Then, the English notation will be displayed instead of the Japanese notation, and page nation will be implemented.
If you want to customize further
rails g kaminari:views default
When you enter These are created in the view.
You can change the paged view by editing these files. (_gap.html.erb is the "..." part where the page is omitted, _page.html.erb is the page number part, _paginator.html.erb is the whole configuration definition)
It's not that difficult to put on, but I'd like to learn a little more about how to customize it. I would like to do the decoration and customization part again soon. :runner::dash: :runner: :runner::runner::dash:
kikawaka: [Rails] Let's master how to use kaminari!
To check the setting items ...
RAILS GUIDES:Configuring Rails Applications
Reference for decoration
Potechiru: [Rails] How to implement pagination using kaminari
Recommended Posts