blog comments 0 del.icio.us bookmarks 0 diggs 0 Google results 0

6.8
PostRank

I'm Paginating Again

Err the Blog - Home From Err the Blog - Home, 1 year ago, 5 views

We’ve all been paginating for a few months now with will_paginate because it’s such a sweet plugin, but Koz put a call out on Rails Core wondering what the leading pagination plugins are. Somebody tell him to subscribe to Err already.

Pagination is getting tossed out of Rails 2.0, so what better time to define the leader. The first step is to pluginize the classic pagination currently in Rails. That’s kinda boring, so we moved straight away to step two, had Mislav Marohnic merge his will_paginate rewrite into our code base, and slapped a 2.0 label on it. (We’ll eventually do step one, but bear with us)

It’s a major release, because we broke the existing API. Mislav’s rewrite converted the paginate method to return not an array of objects and the current page, but a PaginatedCollection proxy. All that really means is you have less code to write now, but you should update any existing code if you’d like to upgrade.

Here’s the updated way to use the plugin via the README:

app/controllers/posts_controller.rb
@posts = Post.paginate :page => params[:page]
app/views/posts/index.html.erb
<p>Handful of posts coming up</p>
<%= render :partial => 'post', :collection => @posts %>

<p>Now let's render us some pagination!</p>
<%= will_paginate @posts %>

The really slick thing going on in the background is that unless you explicitly pass in total_entries, the paginate method with automatically grab the total count based upon the conditions you’ve specified. Nice.

As always, grab the plugin here: ./script/plugin install \ svn://errtheblog.com/svn/plugins/will_paginate

comments

No comments yet.

You must be logged in to add your own comment.