简体   繁体   中英

Rails metasearch search_form with checkboxes

I am a little confused. Despite all questions around this theme here, I can't find the right solution.

What I want to do is to simply add check-boxes to my index filter form.

I am using Metasearch gem and here is my current code :

  <form class="filter_form">
     <%= form_for @search do |f| %>
       <%= f.collection_select :categories_id_equals, Category.all, :id, :name, :include_blank => true, :prompt => "All categories" %>
       <%= f.collection_select :location_id_equals, Location.all, :id, :name, :include_blank => true, :prompt => "All locations" %>

       <ul> 
          <b> Type </b>     
          <% Type.all.each do |type|%>
        <li>
          <%= check_box_tag :types_id_equals, type.id %>
          <%=h type.name %>
        </li>
          <% end %>
      </ul>
       <%= submit_tag "Find Now", :class => "find" %>
     <% end %>

All works fine, except the checkboxes.

I don't have much experience in rails, so I don't really see what I am doing wrong and what could be the most convenient and simplest way.

Update .....................

More explanation - I have a model Trips, which has HABTM relationship with two models ( Categories, Types) and belongs to Location.

I want to be able to filter Trips on it's index by categories (f.collection select) ,location (f.collection select) and types (checkboxes).

After checking types and submitting - nothing changes, no filtering is done!

Here's how I handled it.

<% @sub_categories.each do |cat| %>
   <h2><%= cat.name %> <%= check_box_tag "q[product_category_id_in][]", cat.id %></h2>
<% end %>

Basically just q is whatever your query param is, then right after that in brackets sub in your meta_search method. I used whatever_foreign_key_in since I want to be able to add more than one id to the array to search on. Then add empty brackets after it so rails handles the post params correctly.

<%= check_box_tag "type_ids[]", type.id %>

Will do it for you. The selected ids will be transfered as a string seperated by commatas. You can find them in params[:type_ids] but you have to deal with them manually! Rails is not a magican, its a framework.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM