Search code examples
ruby-on-railsrubyruby-on-rails-4checkboxransack

How build a advanced search with checkbox using Ransack?


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?


Solution

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