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> => params<span class="symbol">[:page</span>],<span class="symbol"> :order</span> => <span class="string">'created_at DESC'</span>
<span class="keyword">end
</span></code> |
and in the template page use
<%= will_paginate @poems, :container => false %>


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 !!
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″.