Thursday, September 20, 2007

How to use Will Paginate 2.0 Rails plugin

Step 1: In the controller:
@users = User.paginate(:order => 'created_at DESC'
:conditions => ["active = ?", 1]
:per_page => 10,
:page => params[:page])

Step 2: In the view:
<%= will_paginate @users, :inner_window => 10, :outer_window => 10 %>

This will create page links from 1 to 10 then dots followed by numbered links for last 10 pages.

Step 3: To make the pagination links sexy, copy the following CSS (Digg style):

div.pagination {
padding: 3px;
margin: 3px;
}

div.pagination a {
padding: 2px 5px 2px 5px;
margin: 2px;
border: 1px solid #AAAADD;

text-decoration: none; /* no underline */
color: #000099;
}
div.pagination a:hover, div.pagination a:active {
border: 1px solid #000099;

color: #000;
}
div.pagination span.current {
padding: 2px 5px 2px 5px;
margin: 2px;
border: 1px solid #000099;

font-weight: bold;
background-color: #000099;
color: #FFF;
}
div.pagination span.disabled {
padding: 2px 5px 2px 5px;
margin: 2px;
border: 1px solid #EEE;

color: #DDD;
}

Reference: Err the blog.

1 comment:

  1. Hi, can you add something in the "step 2" section. That would be fantastic. Cheers.

    ReplyDelete