我生成了一個search_contoller並查看我的rails應用程序。SImple Rails搜索
我的控制器:
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
我查看
<%= 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 %>
我需要的搜索更加具體,並想分手的sql語句,這樣我就可以搜索其他附加的屬性。例如,我希望搜索TITLE(並獲得僅匹配用戶輸入的字符串的結果)或通過GENDER搜索(僅返回用戶搜索的性別)。我怎樣才能做到這一點?我應該爲每種我想執行的搜索類型創建新的form_tags嗎?是否有可能在視圖中只有一個搜索框,並讓用戶在單選按鈕之間選擇他們想要搜索的內容?
您使用的數據庫是? –
即時通訊使用MySQL數據庫。 – Yogzzz