好的?在另一个测试中,我想学习ransack,但我说的是WTF,请看图像:

重复一遍,我知道wHy,我把Product.all放在index.html.erb中:
<%= search_form_for @q do |f| %>
<%= f.label :name_cont, "Name" %>
<br />
<%= f.search_field :name_cont %>
<br />
<%= f.label :hd, "Brand" %>
<br />
<%= f.collection_check_boxes :brand, Product.all, :brand, :brand %>
<br />
<%= f.label :hd, "HD" %>
<br />
<%= f.collection_check_boxes :hd, Product.all, :hd, :hd %>
(...)
<%= f.submit "Search" %>
<% end %>在控制器中有以下内容:
def index
@q = Product.ransack(params[:q])
@products = @q.result
enduniq_value不工作,是错误。我想在搜索节目中点击只有一个值..。例如,有2 500 HD的笔记本注册,想只显示一个复选框值为500 on,并点击,显示两本笔记本,明白吗?谢谢!
发布于 2016-02-23 05:33:28
实现这一目标的一种廉价而肮脏的方法是在特定字段上使用不同的选择,即:
<%= f.collection_check_boxes :brand, Product.select(:brand).distinct, :brand, :brand %>和
<%= f.collection_check_boxes :hd, Product.select(:hd).distinct, :hd, :hd %>这将导致类似于SELECT DISTINCT brand FROM products的SQL查询。
如果可能的话,一个更干净的方法是规范化数据库并创建一个名为Brands的模型,并将产品与品牌联系起来,即属于某个品牌的产品。
https://stackoverflow.com/questions/35551644
复制相似问题