Changing the text displayed when entering simple_form when using an enumeration

I use simple_form to manage my users. To select a user role, I use the input as :: radio_button.

The collection comes from an enumeration in the user model. How to change the text to show something specific, like "Super Admin" instead of super_admin?

_form.html.slim

= form.input :role, collection: User.roles, as: :radio_buttons, item_wrapper_class: 'btn btn-default', checked: User.roles[user.role], required: true

user.rb

  enum role: [:super_admin, :admin, :generic]
+4
source share
2 answers

you can use the option label_methodwith collection

= form.input :role, collection: User.roles, label_method: lambda {|k| k.humanize}, as: :radio_buttons, item_wrapper_class: 'btn btn-default', checked: User.roles[user.role], required: true
+4
source

- , , Simple Form I18n - .

SimpleForm 3.5.0:

:

en:
  simple_form:
    options:
      user:
        role:
          super_admin: 'Super Admin'
          admin: 'Admin'
          generic: 'Regular Joe'

:

<%= f.input :role, collection: User.roles.symbolize_keys.keys %>
0

Source: https://habr.com/ru/post/1608365/


All Articles