Pagination For Ruby on Rails Applications With Pagy
Rendering large or indefinite collection of objects require the basic optimization by implementing pagination. Aside from performance improvements, pagination also reduces server load by fetching and delivering a smaller amount of the data. For the users, it is more convenient to navigate through pages than scrolling through a very long list. Pagination provides a predictable and consistent user interface and transmitting smaller amount of data per request therefore saves bandwidth.
One salient point for pagination is the benefit for SEO. Search engines favor pages that load quickly and with good user experience.
pagy are the top 3 pagination gems respectively in terms of the project score based on The Ruby Toolbox. The project score is determined by the number of downloads from rubygems and the project’s github stars and forks. Unfortunately, the latest version release of
kaminari was 2 years ago.
A couple of developers made a project to benchmark and compare the performances of top 3 pagination gems.
pagy so far outperforms
will_paginate on a lot of factors.
Let’s get started! If you have an existing application using either
kaminari, follow this migration steps to get
pagy up and running on your project. For new projects, follow along for the quick start guide:
If you are using Bundler, add the gem in the Gemfile, like so:
gem 'pagy', '~> 6.2' # check rubygems.com for the latest version, omit patch digit
Download the configuration file and save it into the
Add a line for ApplicationController, like so:
class ApplicationController < ActionController::Base
And use the
pagy method in your controllers.
@pagy, @records = pagy(Product.some_scope)
Render the Pagination
On the ApplicationHelper file, add the line:
<%== pagy_nav(@pagy) %>
That easy! Visit the Pagy website, for more customization, how-to’s and all the API documentation.
Thanks for reading, no matter your software problem, we provide a complete solution to scope, design, develop, test, host, maintain and support it 24/7/365. Contact us to discover how we can bring your ideas to reality!