简体   繁体   English

Rails,searchlogic选择带有复选框的类别

[英]Rails, searchlogic choose categories with checkboxes

I am useing searchlogic to search some paintings. 我正在使用searchlogic搜索一些画作。 Each painting belong to a single category. 每幅画都属于一个类别。 What I would like to do is add multiple checkboxes to my search form, so that users can mark multiple categories. 我想做的是在我的搜索表单中添加多个复选框,以便用户可以标记多个类别。 (joined with or) Is this possible with searchlogic? (与或结合)使用searchlogic可能吗? The query I am looking for is something like this: 我正在寻找的查询是这样的:

SELECT * FROM paintings WHERE category LIKE "white" OR category LIKE "red"...

f.check_box :category (white)
f.check_box :category (black)
f.check_box :category (red)
f.check_box :category (green)

etc. 等等

Here's the super duper easy way to do it (assuming your search object is @search): 这是超级简单的方法(假设您的搜索对象是@search):

f.check_box :category_equals_any, {:name => "search[category_equals_any][]"}, "white"
f.check_box :category_equals_any, {:name => "search[category_equals_any][]"}, "black"
f.check_box :category_equals_any, {:name => "search[category_equals_any][]"}, "red"
f.check_box :category_equals_any, {:name => "search[category_equals_any][]"}, "green"

Could you please see if something like that works in your case? 请问您这种情况是否可行? I'm willing to put some time into this if nobody knows a better solution: 如果没有人知道更好的解决方案,我愿意花一些时间:

I'm looking forward to hear your feedback ;)! 我期待收到您的反馈;)!

in app/helper/conditions_builder.rb 在app / helper / conditions_builder.rb中

class ConditionsBuilder < ActionView::Helpers::FormBuilder
  def multiple_select(collection, possibilities)
    content = []
    id = "#{object_name}[#{collection}][]"
    content << @template.hidden_field_tag(id, "")

    ids = Set.new(self.object.send(collection))
    possibilities.each do |p, label|
      uid = @template.sanitize_to_id("#{id}#{p}")
      checked = ids.include?(p)
      content <<  @template.content_tag("div",
                  @template.check_box_tag(id, p, checked, :id => uid) + " " +
                  @template.label_tag(uid, label))
    end

    return content.join(" ")
  end
end

in app/helper/application.helper add: 在app / helper / application.helper中添加:

def conditions_form(&blk)
  form_for @search, :builder => ConditionsBuilder, &blk
end

In your view you add: (in my case I use haml) 在您看来,您添加:(在我的情况下,我使用haml)

 - conditions_form do |f|
   - f.fields_for @search.conditions do |s|
     = s.label 'Name'
     = s.text_field 'name_contains'
     = s.multiple_select :state_equals, ['open', 'active', 'gone'].collect{|s| [s, _state(s)]}
     = f.submit _("Search"), :class => 'buttons'

In my case I set some defaults: (not sure if this is the best way to do that): 在我的情况下,我设置了一些默认值:(不确定这是否是最好的方法):

  unless params[:search]
    @search.conditions.state_equals = ['open', 'active']
  end

Is the search happening through a POST request? 搜索是否通过POST请求进行? If so you can do it like this... 如果是这样,您可以这样做...

<% for category in %w[white red black] %>
  <p>
    <%= check_box_tag "categories[]", category, (params[:categories] && params[:categories].include?(category)) %>
    <%=h category %>
  </p>
<% end %>

And then perform a search like this.. 然后执行这样的搜索。

Painting.all(:conditions => { :category => params[:categories] })

BTW, I recommend extracting out the category column into a Category model which has id and name columns. 顺便说一句,我建议将类别列提取到具有idname列的Category模型中。 You can then use category_id in paintings to link up the association. 然后,您可以在绘画中使用category_id来链接关联。 This way the database is normalized and you can, for example, easily rename and add a category. 通过这种方式对数据库进行规范化,例如,您可以轻松地重命名并添加类别。 You could also loop through them all with Category.all . 您也可以使用Category.all遍历它们。

I use this in the view 我在视图中使用它

        <%= f.collection_select :category_id_equals, Category.all, :id, :name, :include_blank => true %>

In my controller i use simple 在我的控制器中,我使用简单

  @search = Offer.search(params[:search])
  @search.order ||= "descend_by_id"         
  @offers = @search.all.paginate(:per_page => 20, :page => params[:page])
  @offers_count = @search.count

My object offer has one field category_id, into controller he build the search and I use paginate over the results. 我的对象提供了一个字段category_id,他在控制器中构建了搜索,然后对结果使用了分页。

I hope it help somebody :) 我希望它可以帮助某人:)

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

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