Pagy – New Rails Pagination Gem and How to Implement

Pagy - The ultimate pagination ruby gem

There are two Gems which are very famous in the Rails World for Pagination.

  1. Kaminari
  2. Will paginate

These both gems are widely used because of their simple and easy configuration.

Today, we are going to learn about a new gem for pagination.

Pagy is way much faster than Kaminari and Will paginate. Its size and simplicity are one of the reasons for its stunning performance.

Including all the features, validations, exceptions, etc. Pagy is just 3 small modules of just ~100 lines in total, easy to understand and use. The code contains just three main modules

  1. Pagy
  2. Pagy::Backend
  3. Pagy::Frontend

Features

Read Also: Most useful Ruby on Rails Gems

Installation & Use

As we do for other gems write below the line in gem file and hit bundle install command.

gem 'pagy'

Include back-end module in the controller where you want to use pagination

include Pagy::Backend

Use the pagy method on returning results of the query as below

@pagy, @records = pagy(Product.limit(10))

Include front-end helper in some helper module as below

include Pagy::Frontend

Render the navigation links in view template as below

<%= pagy_nav(@pagy) %>

or with a partial:

<%= render 'pagy/nav', locals: {pagy: @pagy} %>

You can customize some defaults configuration globally by creating pagy.rb file in initializer directory

You can copy the comprehensive and annotated pagy.rb and uncomment and edit what you may need. The file also contains all the relevant documentation links.

Pagy supports other front-end libraries which Kaminari and Will paginate supports like

Bootstrap, Materialize, Foundation, Bulma, etc.

Comparison with other Gems

The best way to quickly get an idea about Pagy is by comparing it to the other well-known gems.

The values shown in the charts below have been recorded while each gem was producing the exact same output in the exact same environment.

~ 33x Faster!

~ 26x Lighter!

There is a migration guide available if you want to switch from Kaminari or Will paginate.

So, here is the new choice of a gem for pagination which is worth to use in your next project.

Click here for more details…


At BoTree Technologies, we build enterprise applications with our RoR team of 25+ engineers.

We also specialize in RPA, AI, Python, Django, JavaScript and ReactJS.

Consulting is free – let us help you grow!

Related posts

Comparing Kivy and BeeWare: Understanding the Key Differences

by Parth Barot
4 years ago

Ruby Best Practices to Follow in Daily Coding

by Ankur Vyas
10 years ago

How to add Affiliation features to Spree Commerce

by Hunny Jummani
6 years ago
Exit mobile version