2012-06-17 140 views
0

我在我的應用程序中實現了一個簡單的導軌搜索。當時我做到了,我只需要完成這項工作,但我確信我做這件事的方式遠非DRY,因此對於如何改進這一點的任何建議將不勝感激。我基本上想要一個搜索文本框,用4個單選按鈕來選擇他們想要搜索的內容。帶有單選按鈕的導軌搜索表單以選擇搜索條件

我目前的指數:

<%= form_tag("/search", :method => "get") do %> 
    <%= label_tag(:q, "Search for Product Title:") %> 
    <%= text_field_tag(:q) %> 
    <%= submit_tag("Search Title") %> 
<% end %> 
<br> 
<%= form_tag("/search", :method => "get") do %> 
    <%= label_tag(:d, "Search for Product Description:") %> 
    <%= text_field_tag(:d) %> 
    <%= submit_tag("Search Description") %> 
<% end %> 
<br> 
<%= form_tag("/search", :method => "get") do %> 
    <%= label_tag(:a, "Search for Category Names:") %> 
    <%= text_field_tag(:a) %> 
    <%= submit_tag("Search Category Name") %> 
<% end %> 
<br> 
<%= form_tag("/search", :method => "get") do %> 
    <%= label_tag(:i, "Search for ID Str:") %> 
    <%= text_field_tag(:i) %> 
    <%= submit_tag("Search ID Str") %> 
<% end %> 

<<results from search here>> 

控制器:

class SearchController < ApplicationController 

    def index 
    if params[:commit] == "Search Title" 
     # Great, now lets figure out what sort of query we're after 
     @searchQuery_title = params[:q] 
     @resultsReceived_title = true 
     if 
     @sqlResults_title_published_size = Product.where("title like ? AND published like ?", "%" + params[:q] + "%", '1') 
     @sqlResults_title_size = Product.where("title like ? OR categories like ?", "%" + params[:q] + "%", "%" + params[:q] + "%") 
     @sqlResults_title = Product.where("title like ? OR categories like ?", "%" + params[:q] + "%", "%" + params[:q] + "%").page(params[:page]).per(200).order("published DESC") 
     end 
    else 
     @resultsReceived_title = false 
     @sqlResults_title = [] 
    end 

    if params[:commit] == "Search Description" 
     # Great, now lets figure out what sort of query we're after 
     @searchQuery_descr = params[:d] 
     @resultsReceived_descr = true 
     if 
      @sqlResults_descr_published_size = Product.where("long_descr like ? AND published = ?", "%" + params[:d] + "%", '1') 
      @sqlResults_descr_size = Product.where("long_descr like ?", "%" + params[:d] + "%") 
      @sqlResults_descr = Product.where("long_descr like ?", "%" + params[:d] + "%").page(params[:page]).per(200).order("published DESC") 
     end 
     else 
     @resultsReceived_descr = false 
     @sqlResults_descr = [] 
     end 

     if params[:commit] == "Search Category Name" 
      # Great, now lets figure out what sort of query we're after 
      @searchQuery_cat = params[:a] 
      @resultsReceived_cat = true 
      if 
      @sqlResults_cat_published_size = Product.where("category_names like ? AND published = ?", "%" + params[:a] + "%", '1') 
      @sqlResults_cat_size = Product.where("category_names like ?", "%" + params[:a] + "%") 
      @sqlResults_cat = Product.where("category_names like ?", "%" + params[:a] + "%").page(params[:page]).per(200).order("published DESC") 
      end 
     else 
      @resultsReceived_cat = false 
      @sqlResults_cat = [] 
     end 

     if params[:commit] == "Search ID Str" 
      # Great, now lets figure out what sort of query we're after 
      @searchQuery_idstr = params[:i] 
      @resultsReceived_idstr = true 
      if 
       @sqlResults_idstr_published_size = Product.where("id_str like ? AND published = ?", "%" + params[:i] + "%", '1') 
       @sqlResults_idstr_size = Product.where("id_str like ?", "%" + params[:i] + "%") 
       @sqlResults_idstr = Product.where("id_str like ?", "%" + params[:i] + "%").page(params[:page]).per(200).order("published DESC") 
      end 
      else 
      @resultsReceived_idstr = false 
      @sqlResults_idstr = [] 
      end 

    # Render the page when we're done. 
    respond_to do |format| 
     format.html 
    end 

    end 
end 

而且是有可能做一個Product.where( 「???標題,如OR類別,如並公佈=」 ,「%」+ params [:q] +「%」,「%」+ params [:q] +「%」,'1').page(params [:page])。per(200)。 order(「published DESC」)

回答

0

表格 4種形式是多餘的。使用參數:in和搜索項:q直接發送表單中的單選按鈕。

<%= form_tag("/search", :method => "get") do %> 
    <%= label_tag(:q, "Search:") %> 
    <%= text_field_tag(:q) %> 
    <% [ 'product title', 'product description', 'categories', 'ID string' ].each do |in| %> 
    <br> 
    <%= radio_button_tag 'in', in %> 
    <%= in.humanize %> 
    <% end %> 
    <%= submit_tag("Search") %> 
<% end %> 

如果你堅持把不同的按鈕文本各類型的搜索,請使用單選按鈕一個JavaScript鉤子去改變它。

控制器。 在模型中將搜索抽象爲命名範圍(請參見下文)。由於每種類型的搜索都收集相同的產品型號實例,因此在Product上定義整個搜索範圍的最佳做法是。然後你的控制器看起來像:

def index 
    @result_scope = Product.search(params[:in],params[:q]) 
    @results = @result.page(params[:page]).per(200).order("published DESC") 
    @results_size = @result.count 
    @results_published_size = @result.published.size 
end 

我省略resultreceived等變量,因爲可以在知道params[:in]的觀點來制定。爲此使用一個輔助方法,然後在你的索引視圖模板中調用它。您也可以通過分析來了解哪些結果集放入了@results。 (如果你需要知道的話,因爲無論我如何搜索它們,可能都會以相同的方式呈現結果產品。)所以我只用一個變量將結果傳遞給視圖。如果您使用的軌道默認渲染

這部分是在動作不必要的和您的視圖模板具有正確的道路(這就是所謂的隱式渲染):

# Render the page when we're done. 
respond_to do |format| 
    format.html 
end 

模式

class Pruduct < ActiveRecord::Base 
    # ... 
    scope :search, lambda { |q,in| 
    #... 
    } 
    scope :published, where(published: true) 
end 

希望這可以幫助。