带复选框的 Rails 元搜索 search_form
我有点困惑。 尽管这里有围绕这个主题的所有问题,但我找不到正确的解决方案。
我想要做的就是简单地将复选框添加到我的索引过滤器表单中。
我正在使用 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 %>
除了复选框之外,一切正常。
我在 Rails 方面没有太多经验,所以我真的不明白我做错了什么以及什么是最方便和最简单的方法。
更新 更多
解释 - 我有一个模型 Trips,它与两个模型有 HABTM 关系( 类别、类型)并且属于位置。
我希望能够按类别(f.collection select)、位置(f.collection select)和类型(复选框)过滤其索引上的行程。
检查类型并提交后 - 没有任何变化,没有进行过滤!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我是这样处理的。
基本上,q 就是您的查询参数,然后是您的 meta_search 方法中括号内的 sub 。我使用了whatever_foreign_key_in,因为我希望能够将多个id添加到数组中进行搜索。然后在其后添加空括号,以便 Rails 正确处理后参数。
Here's how I handled it.
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.
会为你做的。选定的 ID 将作为用逗号分隔的字符串进行传输。您可以在
params[:type_ids]
中找到它们,但您必须手动处理它们! Rails 不是魔术师,它是一个框架。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.