simple_form collection labels

Alexei Danchenkov picture Alexei Danchenkov · May 23, 2012 · Viewed 9.1k times · Source

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}")}.

Answer

DVG picture DVG · May 24, 2012

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}") }