Pagination in Rails With the will_paginate Gem

The world is spinning faster and faster and this acceleration is evident in all facets of our lives. Especially when it comes to business, the premium is on the speed. In this frantic accelerated, volatility is the only constant and people have great affinity towards things that can save time. The popularity of Ruby on Rails is owing to the fact that what could take months and years for other programming languages to complete can be built in weeks and days with Ruby on Rails. So how does Ruby on Rails manage to build applications in such short time? Among other things, Ruby gems are one of most important contributing factors in making Ruby on Rails reduce development overheads. These gems are a list of categorized reusable codes helping the developers build application at a faster rate. One of the most important gem among a horde of gems is pagination gem. Pagination which is a really important aspect of any web application help in dividing documents into discrete pages. In Ruby on Rails we can easily paginate data using a gem called ‘will_paginate’. The will_paginate library makes adding pagination functionality to Rails apps (and other Ruby frameworks) effortless. will_paginate is very well designed plugin. Besides ActiveRecord object integration, it can integrate with array and any collection.

Steps for integration of will_paginate with Rails :

Step 1 : Add “gem will_paginate” in your Gemfile

Gemfile:

gem ‘will_paginate’, ‘~> 3.0’

Step 2 :  Bundle Install

Perform “Bundle Install” of your Rails Project

Bundle Install :

This command has to be executed in Rails Application directory which will install gem ‘will_paginate’ in Ruby environment if it is not installed already. If this gem is already available in your environment then it will just use the gem will Rails Application.

Step 3 : Basic Integration

Add pagination parameter to the Model which is being queried for the Paginated result set. Now, let’s add pagination to our application. Open your controller again and modify it so it looks like the below code.

Contoller code :

app/controllers/articles_controller.rb:

class ArticlesController < ApplicationController

def index

@articles = Blog.paginate(:page => params[:page], :per_page => 10)

end

end

It would add required parameters in the ensuing collection of records to display pagination links in front-end (.erb). Parameters that are added:

  • current_page – it is the current page number for paginated result data set
  • total_entries – numbers of records in database which satisfies the given criteria
  • limit – per page limit for the paginated result data
  • offset – current paginated data set -> to show current page

Parameters :

  • :page – This is parameter sent in Query String. Based on this, which records are to be fetched is decided.
  • :per_page – This is Number of results that you want to fetch per page

View Code :

app/views/articles/index.html.erb

<div class=”row margin-twenty5-zero margin-b-zero pagination-row”>

<div class=”col-lg-8 col-md-8 col-sm-7 col-xs-6 padding-zero”>

<ul class=”pagination”>

<li class=”disabled”> <%= will_paginate(@articles,:previous_label => “&laquo;”, :next_label => “&raquo;”,:class=>”small-font”,:outer_window => 1,:inner_window => 0) %></li>

</ul>

</div>

<div class=”col-lg-4 col-md-4 col-sm-5 col-xs-6 pagination-info”>

<span class=”pull-right”><%= page_entries_info(@articles) %></span>

</div>

</div>

Standard View Helpers :

Returns HTML representing page links for a WillPaginate::Collection-like object. In case there is no more than one page in total, nil is returned.

  • :class – CSS class name for the generated DIV (default: “pagination”)
  • :previous_label – default: “« Previous”
  • :next_label – default: “Next »”
  • :page_links – when false, only previous/next links are rendered (default: true)
  • :inner_window – how many links are shown around the current page (default: 4)
  • :outer_window – how many links are around the first and the last page (default: 1)
  • :link_separator – string separator for page HTML elements (default: single space)
  • :param_name – parameter name for page number in URLs (default: :page)
  • :params – additional parameters when generating pagination links (eg. :controller => “foo”, :action => nil)
  • :renderer – class name, class or instance of a link renderer (default in Rails: WillPaginate::ActionView::LinkRenderer)
  • :container – toggles rendering of the DIV container for pagination links, set to false only when you are rendering your own pagination markup (default: true)

All options not recognized by will_paginate will become HTML attributes on the container element for pagination links

<%= will_paginate @posts, :style => ‘color:blue’ %>
page_entries_info(collection, options)
Renders a message containing number of displayed vs. total entries.

<%= page_entries_info @posts %>
#=> Displaying posts 6 – 12 of 26 in total

  • :model – a model class or string name (default: collection.first.class)
  • :html – set to false to generate plain text (default: true)

Github : https://github.com/mislav/will_paginate

Read similar articles :

Subscribe For Latest Updates

Related Posts

Leave a Comment

Your email address will not be published. Required fields are marked *

en_USEnglish