问题
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