facet_for is a collection of FormBuilder helpers that speed up the process of creating complex search forms with Ransack.
For many searches, it can be as simple as:
<%= f.facet_for(:field_name) -%>
facet_for will use the database column type to choose an appropriate Ransack predicate, then create both the label and field. It will also work for
gem 'facet_for' in your Gemfile
Many options in facet_for can be overridden by passing hash options.
To choose an alternate predicate, pass
:type => :predicate.
Most of the Ransack predicates are supported:
- Contains -
:type => :cont
- Doesn't Contain -
:type => :not_cont
- Starts With -
:type => :start
- Doesn't Start With -
:type => :not_start
- Ends With -
:type => :end
- Doesn't End With -
:type => :not_end
- Is Null -
:type => :null
- Is Not Null -
:type => :not_null
- True -
:type => :true
- False -
:type => :false
- Greater Than or Equal -
:type => :gte
- Less Than or Equal -
:type => :lte
As well as several special cases and meta-predicates specific to facet_for:
- Collection -
:type => :collection
By default, when passing a
belongs_to association into facet_for, it will choose the
:collection predicate. This produces a
collection_select with the unique options for that association.
:type => :collection on a column in the database, rather than an association, it defaults to all unique options.
To pass specific options, use
:collection =>  to pass an array of options. This may either be a flat array of strings (eg
['Blue', 'Red', 'Green']), a flat array of objects (SomeModel.all```), or a nested array of name and value pairs.
- Contains Any -
:type => :cont_any
:type => :cont_any behaves in a nearly identical manner to
:type => :collection, except that the collection is rendered as a series of check boxes rather than a
By default, facet_for will determine label text based on a combination of the field name and the predicate. This can be overwritten by specifying
:label => 'Your Label Here'.