【发布时间】:2016-12-06 08:58:11
【问题描述】:
我想使用两个select_tags 进行搜索。实际上,这一切都可以单独工作,但我想在第一个/第二个标签中使用选择一个选项,然后搜索开始,然后 - 在第二个/第一个标签中选择一个选项,然后查看更详细的搜索结果。目前,我有:
产品型号:
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
那么,我怎样才能进行不单独工作的搜索?先谢谢了。
【问题讨论】:
标签: ruby-on-rails search drop-down-menu ruby-on-rails-5