如何使用Ransack使用复选框构建高级搜索? [英] How build a advanced search with checkbox using Ransack?
本文介绍了如何使用Ransack使用复选框构建高级搜索?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我如何构建高级搜索此图像的方式:
how I build a advanced search how this image:
我有我的控制器,还很基本:
I have my controller, very basic yet:
def index
@q = Product.ransack(params[:q])
@products = @q.result(distinct: true)
end
有我的观点:
<%= search_form_for @q do |f| %>
<%= f.label :name_cont, "Name" %>
<br />
<%= f.search_field :name_cont %>
<br />
<%= f.label :brand, "Brand" %>
<br />
<%= f.collection_check_boxes :brand, Product.all, :id, :brand %>
<br />
<%= f.label :hd, "HD" %>
<br />
<%= f.collection_check_boxes :hd, Product.all, :id, :hd %>
<br />
<%= f.label :ram, "RAM" %>
<br />
<%= f.collection_check_boxes :ram, Product.all, :id, :ram %>
<br />
<%= f.submit "Search" %>
<% end %>
但是,唯一可用于搜索的是输入名称。我有两个问题。 1º复选框无效。 2º一些复选框正在重复执行,因为我将Product.all放进去了,但是也放得截然不同,但是没有用= /
您能帮我吗?
But, the only thing that worked on search is the input name. And I have two problems. 1º The checkboxes don't worked. 2º Some checkboxes is repeting because I put Product.all, but too put distinct, but nothing worked =/ Can you help me please?
推荐答案
您可以尝试关注品牌,高清和公羊
you can try following for brand, hd and ram
<% Product.pluck('distinct brand').each do |brand| %>
<%= check_box_tag('q[brand_cont_any][]', brand ) %>
<%= brand %>
<% end %>
这篇关于如何使用Ransack使用复选框构建高级搜索?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文