Implement Rails pagination

Implement pagination

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

Implement Rails pagination
About rails kaminari pagination
[Rails] Implement search function
Implement Rails account BAN
[Rails] Implement rake task
Implement markdown in Rails
Implement application function in Rails
[Rails] Implement User search function
Implement follow function in Rails
Implement LTI authentication in Rails
Implement import process in Rails
[Rails] How to implement scraping
[Rails] Implement image posting function
[Rails 6] Pagination function implementation (kaminari)
[Rails] Make pagination compatible with Ajax
Kaminari --Added pagination function of Rails
Create pagination function with Rails Kaminari
Implement simple login function in Rails
Implement a contact form in Rails
[Rails] How to implement star rating
First pagination feature added in rails
Implement CSV download function in Rails
Implement pagination using SpringData Pageable + Thymeleaf
[Rails g.error]
Rails basics
Implement tail
Rails Review 1
How to implement search functionality in Rails
Rails API
Rails migration
[Rails] first_or_initialize
rails tutorial
About Rails 6
Rails foundation
Rails memorandum
rails tutorial
rails tutorial
[Rails] devise
How to implement ranking functionality in Rails
[Rails] Implement star rating (input, save, display)
rails tutorial
rails tutorial
How to implement image posting using rails
Rails Tips
rails method
rails tutorial
Implement button transitions using link_to in Rails
[Rails] ActiveRecord
[Rails] form_with
Rails Review 2
Implement share button in Rails 6 without using Gem
[Rails6 + Vue.js] Implement CSV import process using axios
Implement star rating function using Raty in Rails6
[Rails] How to implement unit tests for models
How to implement a like feature in Rails
Implement the Like feature in Ajax with Rails.
[Rails] How to easily implement numbers with pull-down
Implement iteration in View by rendering collection [Rails]
Use pagy for pagination in your Rails app.
How to implement Pagination in GraphQL (for ruby)
[Rails] Ranking and pagination in order of likes