will_paginate plugin – Installing and Sample Code

You can install this plugin as a gem but in this example, I’ll install it as a plugin so it can travel with the rails application. This disadvantage of installing it as a plugin instead of a gem is that you’ll need to install it as a plugin for each rails app you want to use it in.


cd [your_rails_app]
script/plugin install git://github.com/mislav/will_paginate.git

Add the something like the following inside your controller’s index action, for example:

    @releases = Address.paginate( :page => params[:page], 
                                  :per_page => 8,
                                  :conditions => ['first_name like ?', "%#{params[:first_name_search]}%"],
                                  :order => 'last_name')    
  • params[:page] will allow the server to know which page you’re viewing
  • :per_page displays N records per page
  • :conditions section allows you to type in first_name_search field in the search field, as described below to limit the records to those which contain the letters you’ve entered in the search field on the index.html.erb page below.

Add the following search field to your index.html.erb file:

<% form_tag releases_path, :method => 'get' do %>
  <p>
    <%= text_field_tag :first_name_search, params[:first_name_search] %>
    <%= submit_tag "Search First Names", :name => nil %>
  </p>
<% end %> 
VN:F [1.9.22_1171]
Rating: 0.0/5 (0 votes cast)
VN:F [1.9.22_1171]
Rating: 0 (from 0 votes)
Facebook Twitter Email

2 Comments to “will_paginate plugin – Installing and Sample Code”