# Quick Start

Ensure to have rack installed (or gem install rack)

Download and run the following file

standalone_app.ru
standalone_app.ru 3.9KB

Install the gem
gem install pagy

Use it fully without any app

# Install

If you use Bundler, add the gem in the Gemfile, optionally avoiding the next major version with breaking changes ( see RubyGem Specifiers):

Gemfile
gem 'pagy', '~> 7.0' # omit patch digit

If you don't use Bundler, install and require the Pagy gem:

Terminal
gem install pagy
Ruby file
require 'pagy'

# Configure

Download the configuration file linked below and save it into the config/initializers dir

pagy.rb
pagy.rb 11.6KB

Download the configuration file linked below and require it when your app starts

pagy.rb
pagy.rb 11.6KB

# Backend Setup

ApplicationController/AnyController
include Pagy::Backend
Controller action
@pagy, @records = pagy(Product.some_scope)

For search backends see: elasticsearch_rails, meilisearch, searchkick, ransack.

You may also use the calendar, countless, geared, incremental, auto-incremental, infinite pagination

# Render the pagination

Include the frontend

ApplicationHelper/AnyHelper
include Pagy::Frontend

Use a fast helper

Helper
<%# Note the double equals sign "==" which marks the output as trusted and html safe: %>
<%== pagy_nav(@pagy) %>

Require the metadata extra

pagy.rb (initializer)
require 'pagy/extras/metadata'

Add the metadata to your JSON response

Controller action
render json: { data: @records, pagy: pagy_metadata(@pagy, ...) }

Require the headers extra

pagy.rb (initializer)
require 'pagy/extras/headers'

Add the pagination headers to your responses

Controller
after_action { pagy_headers_merge(@pagy) if @pagy }

Render your JSON response as usual

Controller action
render json: { data: @records }