Ruby on rails 简单Rails搜索
我为我的rails应用程序生成了一个搜索控件和视图 我的控制器:Ruby on rails 简单Rails搜索,ruby-on-rails,ruby-on-rails-3,ruby-on-rails-3.1,Ruby On Rails,Ruby On Rails 3,Ruby On Rails 3.1,我为我的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] @resultsReceive
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
class SearchController
我的看法
<%= 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 %>
“get”)do%>
结果发现:
发表
产品
product.id,:class=>“发布复选框”}%>
标题:
产品发布时间:
类别名称:
类别:
评级:
真 的:
年龄:
性别:
说明:
...
切换
男性:
回填女性:
男女通用:
MP卖方名称:
价格:当前价格:$/基数:$
身份证号码:
股票:
上一次更新于
我需要更具体的搜索,并希望分解sql语句,以便搜索其他附加属性。例如,我希望搜索标题(并获得仅与用户输入的字符串匹配的结果)或按性别搜索(并仅返回用户正在搜索的性别)。我该怎么做?我是否应该为要执行的每种类型的搜索创建新的form_标记?是否可以在视图中只有一个搜索框,并让用户在单选按钮之间选择要搜索的内容?使用MySQL,您的选项仅限于自行构造LIKE子句。MySQL上的大多数Rails应用程序将使用其他搜索选项,如SOLr(太阳黑子)、ElasticSearch或Sphinx 在PostgreSQL上,您有更多的选项可以使用内置的全文搜索功能,如Texticle 上面有很多链接: