2016-08-01 95 views
0

我想使用兩個select_tag s進行搜索。實際上,這一切都單獨運行良好,但我想在第一個/第二個標籤中選擇一個選項,然後開始搜索,然後 - 在第二個/第一個標籤中選擇一個選項,並查看更詳細的搜索結果。現在,我有:使用2 select_tag進行導軌搜索

產品型號:

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 

所以,我怎樣才能做一個搜索,不會單獨工作?謝謝你。

回答

0
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]) 
0

首先,我相信你應該把兩個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部分paramenter。

> 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