Use pagy for pagination in your Rails app.

at first

The Ruby on Rails 5 Quick Learning Practice Guide that can be used in the field introduces kaminari to realize the pagination function. Here, I would like to use pagy, which is reputed to be 40 times faster than kaminari.

現場で使える Ruby on Rails 5速習実践ガイド

What is pagination?

If you display all the data when there are 100 tasks (tasks, tasks), the browser display will be slow and it will be difficult to scroll. Therefore, if you display 10 or 20 pages separately, you will be able to browse comfortably.

Preparation

Edit the Gemfile for pagy.

# Gemfile
gem 'pagy' #Pagination
% bundle install

Preparation

So that you can use the page division function using pagy Add to ApplicationController and ApplicationHelper.

# app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
  include Pagy::Backend
end
# app/helpers/application_helper.rb
module ApplicationHelper
  include Pagy::Frontend
end

The following is also described so that the page division can be displayed neatly.

# config/initializers/pagy.rb
require 'pagy/extras/semantic'

#The default value is 20, but if you want to page every 10 items, write as follows.
# Pagy::VARS[:items] = 10

It is provided so that you can set various other settings, so you can drop it from the following. https://raw.github.com/ddnexus/pagy/master/lib/config/pagy.rb

Try to search the data in the range corresponding to the page number.

class TasksController < ApplicationController
  def index
    @pagy, @tasks = pagy(Task.all)
  end
end

Display paginated data in a view

== pagy_semantic_nav(@pagy)

table
  thead
    tr
      th = Task.human_attribute_name(:name)
  tbody
    - @tasks.each do |task|
      tr
        td = task.name

Afterword

I wrote it roughly, but I hope it helps someone.

reference

pagy Quick Start

Recommended Posts

Use pagy for pagination in your Rails app.
Use Timecop in Rails web app to travel time on your browser
Use images in Rails
Use multiple checkboxes in Rails6!
How to deploy jQuery in your Rails app using Webpacker
Use your own classes in the lib directory with Rails6
Incorporate Elasticsearch into your Rails app
Create a new app in Rails
Use LocationAwareLogger for your own Logger
First pagination feature added in rails
Deploy your Rails app on Heroku
How to change app name in rails
How to use custom helpers in rails
Change the default timezone for the rails app
How to use MySQL in Rails tutorial
Specify home view in rails app [root]
How to use credentials.yml.enc introduced in Rails 5.2
Use static analysis tools in your Gradle project
Use [Rails] devise Guest user function (for portfolio)
[Rails] How to use select boxes in Ransack
[Rails] How to use Gem'rails-i18n' for Japanese support
Get your version number in the Android app
[Rails] How to use PostgreSQL in Vagrant environment
How to implement Pagination in GraphQL (for ruby)
Remedy for command not found in rails s
[Rails] Ranking and pagination in order of likes
Implement Rails pagination
Group_by in Rails
[Rails] Use jQuery
Use Extend (Concerns) in Rails to standardize Controller processing.
[Rails] Return login result in JSON format (for beginners)
I want to use a little icon in Rails
Implement user follow function in Rails (I use Ajax) ②
What should I use for the testing framework [Rails]
Use different TransactionManager for metadata and step in spring-batch
Use variables for class and id names in haml
Implement user follow function in Rails (I use Ajax) ①
[Swift] Use UserDefaults to save data in the app
Beginners use ubuntu in windows to prepare rails environment