Tuesday, February 26, 2008

3 easy steps to do Rails 2.0 pagination

Forget about the kind of awkward pagination of rails 1.x, now you need 3 easy steps to do Rails 2.0 pagination.

1. Install the pagination plugin:

ruby script/plugin install svn://errtheblog.com/svn/plugins/will_paginate

2. Type the following in your controller:

@products = Product.paginate(:page => params[:page], :per_page => 25)

3. Type the following in your view:

<%= will_paginate @products %>

The better thing comparing to rails 1.x pagination is that it is now easier to understand and remember. Isn't it more conventional?

1 comment:

Proc said...

Hi Xiaobo

I just started learning Rails.
I got the error "undefined method paginate..." and ran the command to install the will_paginate plugin but it returns nothing.

When I start Mongrel and refresh my webpage..I get the same error again

Can u post anything for this