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 %>
    <%= text_field_tag :first_name_search, params[:first_name_search] %>
    <%= submit_tag "Search First Names", :name => nil %>
<% 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”

  1. navid 1 July 2010 at 12:02 am #

    index.html.erb shows now.

    VN:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VN:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
  2. Mike Fischer 9 January 2010 at 7:39 pm #

    Your index.html.erb isn’t showing up in the post, FYI.

    will_paginate is definitely great. On my blog I wrote up a list of the main gems I find myself using over and over (including will_paginate). Have a look if you want… http://www.mfischer.com/wordpress/2009/07/13/essential-ruby-on-rails-plug-ins-and-gems/

    VA:F [1.9.22_1171]
    Rating: 5.0/5 (1 vote cast)
    VA:F [1.9.22_1171]
    Rating: +1 (from 1 vote)

Leave a Reply