Pagination in Ruby on Rails

Old rails has a buitl in pagination option. For example is you have to paginate @user, which is a model object for the table user

def user_list
@user_pages,@users=paginate(:users,:order=>’name’)
end

But in new version of rails, they removed the pagination option. So you need to install a plugin , which name is will_paginate

You can install this plugin using gem

gem install will_paginate

After the installation you have to add require “will_paginate” in config/environment.rb (at bottom)

You have to restart your ruby server after that.

In the controller area use:

1
2
3
4
<code class="ruby"><span class="keyword">def</span> index
@poems = Poem.paginate<span class="symbol"> :page</span> =&gt; params<span class="symbol">[:page</span>],<span class="symbol"> :order</span> =&gt; <span class="string">'created_at DESC'</span>
<span class="keyword">end
</span></code>

and in the template page use

<%= will_paginate @poems, :container => false %>

2 Comments , , ,

2 Responses to “Pagination in Ruby on Rails”

  1. Amal kumar s August 11, 2009 at 11:24 am #

    Thnks it worked well for me.

    pls NOTE one thing add require “will_paginate” in config/environment.rb (at bottom). ie after the ‘end’. Be careful !!

  2. Abdul Gafoor December 22, 2011 at 2:09 pm #

    Thanks for your posting, its working fine, but you missed to include no.of page attribute in the model “cattr_reader :per_page @@per_page = 2″.

Leave a Reply