0

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!

4

2 回答 2

0

以下是我的处理方式。

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

基本上只是 q 是您的查询参数,然后在您的 meta_search 方法中的括号子之后。我使用了whatever_foreign_key_in,因为我希望能够向数组中添加多个ID以进行搜索。然后在它之后添加空括号,以便 rails 正确处理 post 参数。

于 2014-04-08T20:46:19.337 回答
0
<%= check_box_tag "type_ids[]", type.id %>

会为你做的。选定的 id 将作为逗号分隔的字符串传输。您可以在其中找到它们,params[:type_ids]但您必须手动处理它们!Rails 不是魔术师,它是一个框架。

于 2011-12-28T09:53:27.110 回答