activerecord-hackery--ransack/docs/docs/going-further/form-customisation.md

1.6 KiB

sidebar_position title
4 Form customisation

Predicate and attribute labels in forms may be specified with I18n in a translation file (see the locale files in Ransack::Locale for more examples):

# locales/en.yml
en:
  ransack:
    asc: ascending
    desc: descending
    predicates:
      cont: contains
      not_cont: not contains
      start: starts with
      end: ends with
      gt: greater than
      lt: less than
    attributes:
      person:
        name: Full Name
      article:
        title: Article Title
        body: Main Content

The names of attribute fields may also be changed globally or under activerecord:

# locales/en.yml
en:
  attributes:
    model_name:
      model_field1: field name1
      model_field2: field name2
  activerecord:
    attributes:
      namespace/article:
        title: AR Namespaced Title
      namespace_article:
        title: Old Ransack Namespaced Title

To limit the predicates in the predicate_select form helper in a view template, pass an array of permitted predicates with only:

<%= f.predicate_select only: %i(cont not_cont eq not_eq blank null) %>

Compound predicates (_any & _all) may be removed by passing the option compounds: false.

<%= f.predicate_select compounds: false %>

Searchable attributes versus non-searchable ones may be specified as follows:

def self.ransackable_attributes(auth_object = nil)
  %w(searchable_attribute_1 searchable_attribute_2 ...) + _ransackers.keys
end