Will Paginate — Plugin

Will Paginate:

1. Installation:

cmd>git clone git://github.com/mislav/will_paginate.git

2. Git Installation

You don’t have git installed already then install it

Install git from here.

3. Try with this Basic Code:

a. Product model(products Table) contains these fields

id
name
price
description

b. DB has 100 records

c. controller:

def productlist
@products = Product.paginate :page => params[:page]
end

d. productlist.hml.erb

<%= will_paginate @products %>

<ul>
<% @products.each do |c| %>
<li><%= c.id %> – <%= c.name %> – <%= c.price %> – <%=
c.description %></li>
<% end %>
</ul>

Output looks Like:

Links for more help –> http://agilewebdevelopment.com/plugins/will_paginate

Pagination in Rails

Pagination :

Inside method

if params[:Pincode]
session[:location] = params[:Pincode]
end
location = session[:location]
# @locations_pages, @locations = paginate :companies, :per_page =>2, :conditions=>[“location like ?”, “%#{location}%”]

@locations_pages, @zipdetail = paginate :zipdatas, :per_page =>5,:conditions  =>[“Pincode like?”,”%#{location}%”]

View :

<% for c in @zipdetail %>
<table border=’1′>
<tr><td> <%= c.Pincode %> </td>
<td> <%= c.OfficeName %> </td>
<td> <%= c.Status %> </td>
<td> <%= c.Suboffice %> </td>
<td> <%= c.HeadOffice %> </td>
<td> <%= c.OfficeName %> </td>
<td> <%= c.Location %> </td>
<td> <%= c.Telephone %> </td>
<td> <%= c.SPCC %> </td>
<td> <%= c.Postal %> </td>
<td> <%= c.Division %> </td>
<td> <%= c.Region %> </td>
<td> **************** </td>

</tr>
</table>
<% end %>

<%= link_to ‘[previous]’, { :page => @locations_pages.current

.previous } \
if @locations_pages.current.previous %>
<%= pagination_links(@locations_pages) %>
<%= link_to ‘[next]’, { :page => @locations_pages.current.next } \
if @locations_pages.current.next %>

<%= link_to ‘Back to Search’, :action => ‘show’ %>
<%= link_to ‘Home Page’, {:action => ‘index’, :controller => ‘home’}, {:accesskey => 3} %>