DEV Community

Cover image for Pagination without using any gems in Ruby On Rails
Sparsh Garg
Sparsh Garg

Posted on • Edited on

Pagination without using any gems in Ruby On Rails

What is pagination?

Pagination refers to the process of dividing a list of items into multiple pages that can be navigated through using buttons or links. This is commonly used for displaying large sets of data, such as search results or articles, in a more manageable format.

Why to use pagination?

Pagination is used to improve the user experience and performance of a website by dividing large sets of data into smaller, more manageable chunks.

User experience: Large lists of data can be overwhelming for users and make it difficult to find the information they are looking for. Pagination allows users to easily navigate through the data and find the specific information they need. It also allows users to quickly scan through the data to find relevant information.

Performance: Loading large amounts of data at once can slow down a website, especially for users with slow internet connections. Pagination allows for smaller amounts of data to be loaded at a time, improving the overall performance of the website. Additionally, it helps to reduce the load on the server, which can also improve performance.

Accessibility: Pagination also helps with accessibility, because it allows users to easily access the specific data they need and it enables them to skip irrelevant data.

Search Engine Optimization: Search engines can also benefit from pagination because it allows them to crawl and index the content on a website more efficiently.

In summary, Pagination improves the user experience, performance, accessibility, and search engine optimization of a website.

Pagination in Ruby On Rails

In Ruby on Rails, pagination can be implemented using the "will_paginate" gem or the "kaminari" gem. Both of these gems provide a simple and easy way to add pagination to a Rails application.

The "will_paginate" gem adds a paginate method to the ActiveRecord::Relation class, which can be used to paginate any database query. For example, to paginate a list of users, you would write

User.paginate(:page => params[:page], :per_page => 10)
Enter fullscreen mode Exit fullscreen mode

The "kaminari" gem provides similar functionality, with a page method that can be used to paginate any database query. For example, to paginate a list of users, you would write

User.page(params[:page]).per(10)

Enter fullscreen mode Exit fullscreen mode

Once you have paginated your data, both gems provide helper methods for generating the HTML for the pagination links. In the views, you can use the will_paginate or kaminari_paginate helper method to generate the HTML for the pagination links.

Both gems are well-documented and easy to use, and you can choose the one that better fits your needs or preferences.

Additionally, Rails has built-in pagination feature in rails 6, which is called "built-in pagination" using the pagy gem.

But recently I have used one more method of pagination i.e using SQL.

def show_method
     total_page = (ModelName.all.count.to_f / 5).ceil
     current_page = (params[:page] || 1).to_i
     per_page = 5
     records_fetch_point = (current_page - 1) * per_page
     model = ModelName.order(id: :desc).limit(per_page).offset(records_fetch_point)
     page = {
       page_number: current_page,
       per_page: per_page,
       total_page: total_page
     }
     if model.empty?
       render json: {error: 'No data found'}, status: :not_found
     else
       render json: {data: model, message: "Data", meta: page}, status: :ok
     end
   end
Enter fullscreen mode Exit fullscreen mode

I have used this method because :-

  1. I wanted to try some custom method for pagination.
  2. Sometimes gems like pagy, kaminari, will_paginate does not work due to version or configuration issues.

Top comments (0)