product.rb
分页方法:
def self.search(search, page)
paginate :per_page => 5, :page => page,
:conditions => ['name like ?', "%#{search}%"],
:order => 'id'
end
以下是搜索方法:
def self.search(search)
if search
find(:all, :conditions => ['name LIKE ?', "%#{search}%"])
else
find(:all)
end
end
product_controller
@products = Product.search(params[:search],params[:page])
index.html.erb
<%= form_tag products_path, :method => 'get' do %>
<p>
<%= text_field_tag :search, nil, :placeholder => "Search items here" %>
<%= submit_tag "Search", :name => nil %>
</p>
<% end %>
<%= will_paginate @products%>