-
Notifications
You must be signed in to change notification settings - Fork 864
Simple search
Ryan Bates of railscasts.com made a screencast on will_paginate. There he creates a simple search form that displays paginated results. Here is what Ryan wrote in the model:
# models/product.rb
def self.search(search, page)
paginate :per_page => 5, :page => page,
:conditions => ['name like ?', "%#{search}%"], :order => 'name'
end
But, will_paginate changed slightly since this screencast was recorded. When you create a search form, make sure its method is GET
:
<% form_tag request.path, :method => 'get' do %>
<% content_tag :label do %>
Search term:
<%= text_field_tag :search, params[:search] %>
<% end %>
<% end %>
If your form's action is POST (or any other HTTP method) then the search term won't be applied to subsequent page links. In other words, when you follow the "Next page" or any other page link, the search parameter(s) would be lost.
Search forms that post with GET are better practice, anyway. One of immediate benefits is that users are able to bookmark search results. Caching is another benefit: browsers, proxies, etc. are happy to cache content that was a result of a GET request.
- Installation instructions
- API documentation
- Differences between v2.3 - v3.0
- Troubleshooting or Report bugs
- Simple search functionality
- Translating output (i18n)
- Browse the source code
- See the recent commits