How build a advanced search with checkbox using Ransack?

為{幸葍}努か 提交于 2020-01-17 07:32:46

问题


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

And have my view:

<%= 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 %>

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?


回答1:


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 %>


来源:https://stackoverflow.com/questions/35953049/how-build-a-advanced-search-with-checkbox-using-ransack

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!