Monday, December 29, 2008

Pagination with Search




Quite hard, I've spent 2 days figuring this out ( since I'me new to RoR World )

Here is what to do.

1. Install Mislav Will Paginate Plug in ( http://github.com/mislav/will_paginate/wikis/home )
gem install mislav-will_paginate

2. Edit config/environment.rb and add this sentence to very bottom of the page
require 'will_paginate'

3. restart webbrick
script/server

4. Change the index method to include paginate
edit app/controllers/employees_controller.rb

# GET /employees
# GET /employees.xml
def index
if params[:term]
@employees = Employee.paginate(:all,
:conditions => ['first_name LIKE ? OR
last_name LIKE ? OR
email LIKE ? OR
mobile_number LIKE ? OR
description LIKE ? ', "%#{params[:term]}%",
"%#{params[:term]}%",
"%#{params[:term]}%",
"%#{params[:term]}%",
"%#{params[:term]}%"],
:page => params[:page], :per_page => 2 )
else

@employees = Employee.paginate(:page => params[:page], :per_page => 2 )
end

respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @employees }
end
end



5. Edit the view and add this
<%= will_paginate @employees %>

6.Enjoy

No comments:

Post a Comment