How build a advanced search with checkbox using Ra

2019-09-14 15:40发布

how I build a advanced search how this image:

advanced search with checkboxes

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条回答
贼婆χ
2楼-- · 2019-09-14 16:38

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 %>
查看更多
登录 后发表回答