简体   繁体   English

带有复选框的Rails metasearch search_form

[英]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 : 我正在使用Metasearch gem,这是我当前的代码:

  <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. 我在Rails上没有太多经验,所以我真的看不到我在做什么错,什么可能是最方便,最简单的方法。

Update ..................... 更新.....................

More explanation - I have a model Trips, which has HABTM relationship with two models ( Categories, Types) and belongs to Location. 更多说明-我有一个Trips模型,该模型与两个模型(类别,类型)具有HABTM关系,并且属于位置。

I want to be able to filter Trips on it's index by categories (f.collection select) ,location (f.collection select) and types (checkboxes). 我希望能够按类别(f.collection选择),位置(f.collection选择)和类型(复选框)对其索引上的Trips进行过滤。

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. 基本上,q就是您的查询参数,然后紧跟在meta_search方法的方括号子中。 I used whatever_foreign_key_in since I want to be able to add more than one id to the array to search on. 我使用了what_foreign_key_in,因为我希望能够向数组添加多个id进行搜索。 Then add empty brackets after it so rails handles the post params correctly. 然后在其后添加空括号,以使rails正确处理post参数。

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

Will do it for you. 会为你做。 The selected ids will be transfered as a string seperated by commatas. 所选的ID将以逗号分隔的字符串形式传输。 You can find them in params[:type_ids] but you have to deal with them manually! 您可以在params[:type_ids]找到它们,但必须手动处理它们! Rails is not a magican, its a framework. Rails不是魔术师,而是一个框架。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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