Rails 搜索 2 select_tag

Rails search with 2 select_tag

我想使用两个 select_tag 进行搜索。实际上,这一切单独工作都很好,但我想在 first/second 标签中使用选择一个选项,然后开始搜索,然后 - 在 second/first 标签中选择一个选项,然后查看更多详细的搜索结果。现在,我有:

产品型号:

class Product < ApplicationRecord
  belongs_to :position
  has_and_belongs_to_many :size
end

尺码 型号:

class Size < ApplicationRecord
  has_and_belongs_to_many :products
  has_many :items
end

位置型号:

class Position < ApplicationRecord
  has_many :products
end

我的application.html.erb:

<div class="container">
    <div class="row text-center">
      <div class="col-sm-6">
        <%= form_tag(products_path, method: 'GET', remote: true) do %>
          <%= select_tag 'size_id', options_from_collection_for_select(Size.all, 'id', 'title'),
                         onchange: ('this.form.submit();'), class: 'form-control', id: 'filter1', include_blank: true %>
        <% end %>
      </div>
      <div class="col-sm-6">
        <%= form_tag(products_path, method: 'GET') do %>
          <%= select_tag 'position_id', options_from_collection_for_select(Position.all, 'id', 'title'),
                         onchange: ('this.form.submit();'), class: 'form-control', id: 'filter2', include_blank: true %>
        <% end %>
      </div>
    </div>
  </div>

还有一个products_controller:

class ProductsController < ApplicationController
  def index    
    if params[:position_id]
      position = Position.find(params[:position_id])
      @products = position.products
      respond_to do |format|
        format.html
        format.json { render json: @products }
      end
    elsif params[:size_id]
      size = Size.find(params[:size_id])
      @products = size.products
      respond_to do |format|
        format.html
        format.json { render json: @products }
      end
    else
      @products = Product.all
      respond_to do |format|
        format.html
        format.json { render json: @products }
      end
    end
  end
end

那么,我怎样才能进行不能单独使用的搜索呢?先谢谢了。

search_parameters = {
  :position_id => params[:position_id],:size_id => params[:size_id]}
  .select { |key,value| value.present? }

@products = Product.where(
  search_parameters, params[:position_id], params[:size_id])

首先,我认为如果你想让它们一起工作,你应该把它们 select_tags 放在同一个表格中。

  <div class="container">
    <div class="row text-center">
      <%= form_tag(products_path, method: 'GET', remote: true) do %>
      <div class="col-sm-6">
          <%= select_tag 'size_id', options_from_collection_for_select(Size.all, 'id', 'title'),
                         onchange: ('this.form.submit();'), class: 'form-control', id: 'filter1', include_blank: true %>
      </div>
      <div class="col-sm-6">
          <%= select_tag 'position_id', options_from_collection_for_select(Position.all, 'id', 'title'),
                         onchange: ('this.form.submit();'), class: 'form-control', id: 'filter2', include_blank: true %>
      </div>
      <% end %>
    </div>
  </div>

然后您需要更改您的控制器以使用您拥有的参数:

def index
  @products = Product.scoped  # or Product.all if you are using Rails 4.0.13 or newer
  @products = @products.where(position_id: params[:position_id]) if params[:position_id].present?
  @products = @products.where(size_id: params[:size_id]) if params[:size_id].present?
  respond_to do |format|
    format.html
    format.json { render json: @products }
  end
end

这样,控制器将只添加您发送到 SQL 命令的 WHERE 部分的参数。

> Product.scoped
  SELECT `products`.* FROM `products` 

> Product.scoped.where(position_id: 1)
  SELECT `products`.* FROM `products` WHERE `products`.`position_id` = 1

> Product.scoped.where(size_id: 1)
  SELECT `products`.* FROM `products` WHERE `products`.`size_id` = 1

> Product.scoped.where(position_id: 1).where(size_id: 1)
  SELECT `products`.* FROM `products` WHERE `products`.`position_id` = 1 AND `products`.`size_id` = 1