** 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
First listed in the Gemfile to install kaminari
Gemfile
gem 'kaminari'
And the usual bundle install
$ bundle install
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])
.
Now that you can get posts for each page, let's implement it to move pages. It looks like this:
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!
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