Alexei Danchenkov
Alexei Danchenkov

Reputation: 2082

simple_form collection labels

I want to use labels for collection items (for example, their translations).

= simple_form_for(@client) do |f|
  = f.error_notification
    .form-inputs
      = f.input :name, label: t('client.name')
      = f.input :level, collection: ['a', 'b', 'c'],
        label_method: ????
        label: t('client.level')
    .form-actions
      = f.button :submit, value: t('client.submit')

I am trying to figure out, what to use for label_method above. I would like to have label_method to map to translations as in map {|s| t("client.#{s}")}.

Upvotes: 16

Views: 10301

Answers (1)

DVG
DVG

Reputation: 17480

If you don't already have a label method, you can pass a lambda to get the result you want

label_method: ->(obj){ t("client.#{obj}") }

Upvotes: 42

Related Questions