I generated a search_contoller and view for my rails app.
My Controller:
class SearchController < ApplicationController
def index
if params[:commit] == "Search"
# Great, now lets figure out what sort of query we're after
@searchQuery = params[:q]
@resultsReceived = true
if
@sqlResults = Product.where("title like ? OR categories like ?", "%" + params[:q] + "%", "%" + params[:q] + "%").order("published DESC")
end
else
@resultsReceived = false
@sqlResults = []
end
# Render the page when we're done.
respond_to do |format|
format.html
end
end
end
My View
<%= form_tag("/search", :method => "get") do %>
<%= label_tag(:q, "Search for:") %>
<%= text_field_tag(:q) %>
<%= submit_tag("Search") %>
<% end %>
<% if @resultsReceived == true %>
<b><%= @sqlResults.length %> Results Found For: </b><%= @searchQuery %>
<br>
<table>
<tr>
<th>Publish</th>
<th>Product</th>
<th></th>
</tr>
<% @sqlResults.each do |product| %>
<tr class="<%= cycle('oddrow', 'evenrow') %>">
<td><%= check_box_tag "product_ids[]", product.id, product.published, {:id => product.id, :class => "publish_checkbox"} %></td>
<td><%= link_to 'Edit', edit_product_path(product) %></td>
<td><%= link_to 'Destroy', product, confirm: 'Are you sure you want to KILL this product?', method: :delete %></td>
<td>
<div>
<b>Title:</b> <%= product.title %>
</div>
<div class="float_left prod_image">
<a href='<%= product.url %>' target="_blank"><img src='<%= product.image_url %>' class='product_image'></img></a>
</div>
<div class="float_left prod_description">
<div><b>Product is Published: </b> <%= product.published %></div>
<div><b>Category Names:</b> <%= product.category_names %></div>
<div><b>Categories:</b> <%= product.categories %></div>
<div><b>Rating:</b> <%= product.rating %></div>
<div><b>Wow:</b> <%= product.is_curated %></div>
<div><b>Age:</b> <%= product.pr_attributes['ag_type'] %></div>
<div><b>Gender:</b> <%= product.pr_attributes['gender_type'] %></div>
<div>
<b>Description:</b>
<% if product.long_descr.length < 200 %>
<%= product.long_descr %>
<% else %>
<span id="short_desc_<%= product.id %>"><%= product.long_descr[0..199] %>...</span>
<span class="long_description", id="long_desc_<%= product.id %>"><%= product.long_descr %> </span><a id="<%= product.id %>" class="toggle">toggle</a>
<% end %>
</div>
<div><b>Backfill Male:</b> <%= product.backfill_text['male'] %></div>
<div><b>Backfill Female:</b> <%= product.backfill_text['female'] %></div>
<div><b>Backfill Unisex:</b> <%= product.backfill_text['unisex'] %></div>
<div><b>MP Seller Name:</b><%= product.mp_seller_name %></div>
<div><b>Price:</b> Current: $<%= product.curr_item_price %> / Base: $<%= product.base_item_price %></div>
<div><b>ID STR:</b> <%= product.id_str %></div>
<div><b>Stock:</b> <%= product.stock_status %></div>
<div><b>Last Updated by <%= product.updated_by_name %> at <%= product.updated_at %></b></div>
</div>
</tr>
<% end %>
<% end %>
I need the search to be more specific, and would like to break up the sql statement, so I can search for other additional attributes. For example, I would like search for either TITLE (and get results that match only the string entered by the user) or search by GENDER (and get back only the gender I user is searching for). How can I do this? Should I create new form_tags for each type of search I want to perform? Is it possible to have just one search box in the view, and have the user select between radio buttons as to what they want to search for?
With MySQL, your options are pretty limited to self constructing the LIKE clauses. Most Rails apps on MySQL will use other search options, like SOLr (sunspot), ElasticSearch, or Sphinx.
On PostgreSQL, you have many more options to use the built in full text search capabilities, like Texticle.
A lot of links to the above: https://www.ruby-toolbox.com/categories/rails_search