will_paginate plugin – Installing and Sample Code
Installing the will_paginate Plugin
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
will_paginate plugin sample code
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 %>
2 Comments to “will_paginate plugin – Installing and Sample Code”