Rails搜索表单,带有单选按钮,用于选择搜索条件

时间:2012-06-17 21:08:06

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.1

我在我的应用中实现了简单的rails搜索。在我这样做的时候,我只需要完成工作,但是我确信我做的方式远非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类别如? AND published =?”,“%”+ params [:q] +“%”,“ %“+ params [:q] +”%“,'1')。page(params [:page])。per(200).order(”已发布的DESC“)

1 个答案:

答案 0 :(得分: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中。 (如果你需要知道,因为你可能以相同的方式呈现结果产品,无论我如何搜索它们。)所以我只使用一个变量将结果传递给视图。

如果您使用rails默认渲染并且您的视图模板具有正确的路径(这称为隐式渲染),则此部分在操作中是不必要的:

# 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

希望这有帮助。