Implement pagination using kaminari. Add kaminari to gemfile and install
After that, create the kaminari configuration file and template.
$ bin/rails g kaminari:config
$ bin/rails g kaminari:views default
You now have a configuration file. Let's take a look at the configuration file
There are some written, You can set the number of items per page with config.default_per_page.
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
Next, modify the action. For example, to list tweets, just do as follows.
controller.rb
@tweets = Tweet.all.page(params[:page])
Finally, for the part where you want to insert pagination in the view part
index.html.erb
<%= paginate @events %>
It was easy to add. I've implemented pagination with spring boot I have some time-consuming memories. Rails is that easy.
That's all for today
Recommended Posts