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.
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.
Edit the Gemfile for pagy.
# Gemfile
gem 'pagy' #Pagination
% bundle install
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
class TasksController < ApplicationController
def index
@pagy, @tasks = pagy(Task.all)
end
end
== pagy_semantic_nav(@pagy)
table
thead
tr
th = Task.human_attribute_name(:name)
tbody
- @tasks.each do |task|
tr
td = task.name
I wrote it roughly, but I hope it helps someone.
Recommended Posts