SimpleSearch

SimpleSearch privides search methods for your ActiveRecord models, by providing methods that allow you to search, group, and order. Main goal is to simplify all search related operations from url.

Getting Started

In your Gemfile:

gem "simple-search"  # Last officially released gem

In your controller:

def index
  @posts = Post.select('posts.*, comments.id as comment_id, comments.body as comment').joins(:comments)
  @posts = @posts.simplesearch(params)
  render :index
end

In your view:

<%= form_tag('/posts', :method=>:GET) do -%>
  ID &gt; <%= text_field_tag 'id_gt', params[:id_gt] %> <br/>
  Subject contains <%= text_field_tag 'subject_ct', params[:subject_ct] %> <br/>
  <%= submit_tag "Search" %>
<% end -%>

<style>
  #pages a {border:1px solid grey; display:inline-block; min-width:13px; text-align:center; text-decoration: none}
  #pages a:first-child, #pages a:last-child {background-color:grey}
  #pages a.current {color:white; background-color:#333}
</style>
<div id="pages">
  Pages : <%=raw page_urls(@posts, :num_pages=>5).join(" ") %>
</div>

<table>
  <tr>
    <th><%=order_link(:id)%></th>
    <th><%=order_link(:subject)%></th>
    <th><%=order_link(:body)%></th>
    <th><%=order_link(:comment_id)%></th>
    <th><%=order_link(:comment)%></th>
  </tr>
<% @posts.each do |post| %>
  <tr>
    <td><%=h post.id %></td>
    <td><%=h post.subject %></td>
    <td><%=h post.body %></td>
    <td><%=h post.comment_id %></td>
    <td><%=h post.comment %></td>
  </tr>
<% end %>
</table>

Search postfixes

Sorting your result

Paging

Grouping

Copyright © 2012 Allen Kim. See LICENSE.txt for further details.