Saturday, September 6, 2008

Pagination in rails using will_paginate

Pagination is a technique of presenting large amount of data on the UI, allowing the users to navigate between pages/data in an easy way.
Being a java developer for so many years, I remember doing dirty logic to achieve pagination to using some very useful and advanced tag libraries like displaytag. What ever may be the technique, pagination is never been an easy task.
But when I started playing around with rails and I got a situation where I need to do some pagination. And I readily found a gem will_paginate and doing pagination on rails is a breeze.

Lets get started first by installing the gem.





sudo gem install will_paginate

To check whether the plugin installed successfully or not, you can do the following in your rails console.



1
2
3
defined? WillPaginate
[].paginate
ActiveRecord::Base.respond_to? :paginate


Now lets add pagination support to an imaginary page listing all the clients



1
2
3
  def list_clients
@clients = Client.paginate(:page => params[:page], :per_page => 5)
end
and then render the pagination in the ui using this simple snippet



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<h3>Clients</h3>

<%= will_paginate @clients %>
<!--Loop through all the clients and printem-->
<table width=100% cellpadding=0 cellspacing=0 border=0>
<% for @client in @clients do%>
<tr class='client_details'>
<td align='center'><%=@client.client_name.upcase%><
/
td>
<td>
<%=@client.address_line_1%>,<%=@client.address_line_2%><br />
<%=@client.city%>,<%=@client.state%>,<%=@client.zip%>
<
/
td>
</tr>
<%end%>
<
/
table>


The code <%= will_paginate @clients %> will add the links to previous/next page along with the page numbers to naviage.
The next few lines of code is just to print the client details.

7 comments:

Emeka said...

Great!

Mittal said...

can you post some screen shots ?

vandawonhaberberger said...

Titanium melting point on the market - TikTok
See more of rocket league titanium white TikTok's collection babyliss pro nano titanium hair dryer of hot titanium pans new titanium 6al4v dishes, everquest titanium which will be featured on December 5th. of Apple Honey Habanero Pepper Puree on Tapatio Mango Hot Sauce.

noandeau said...

v131o9gctbk256 japanese sex dolls,vibrators,sex chair,wolf dildo,Rabbit Vibrators,pink dildoe,realistic dildos,vibrators,wholesale sex toys t236r2lfspb788

Anonymous said...

m664c4hldtg033 prostate massagers,dog dildo,Bullets And Eggs,dildo,dildo,japanese sex dolls,vibrators,dildos,real dolls t529s8mjzlq727

sarrit said...

v158p2qhrzp320 sex chair,anal toys,huge dildos,sex toys,adult sex toys,anal vibrators,vibrators,male masturbators,Male Masturbators a381b9hjzwv291

Anonymous said...

Naturally, we can to} treat the altering cumulative net income of 카지노 a player during their playing actions as a random stroll process8. We are notably fascinated in the diffusive traits of the gambler’s net income. Within this paper, we'll mainly give attention to} the evaluation on the population degree. In a earlier research of pores and skin gambling8, we have shown that in a parimutuel betting sport , a gambler’s net income displays a crossover from superdiffusion to regular diffusion. Offering the last word|the last word} VIP stay gaming expertise online, our Salon Privé is a super-elegant, top-end VIP environment.