gotqn
gotqn

Reputation: 43636

format complex hash - simple_form gem

I have a nested hash like this:

  LANGUAGE_DETAILS =  {
                       BG: {
                              Name: 'Български',
                              Flag: ''
                           },
                       EN: {
                              Name: 'English',
                              Flag: ''
                           },
                       RU: {
                              Name: 'Руский',
                              Flag: ''
                           },
                       UK: {
                              Name: 'Украински',
                              Flag: ''
                           }
                    }

and need to format it like the following hash:

 {
      BG: 'Български',
      EN: 'English',
      RU: 'Руский',
      UK: 'Украински'
  }

in order to use it as simple_form input parameter like this:

<%= f.input :language_code, collection: SecurityUser::LANGUAGE_DETAILS,
            label_method: :last,
            value_method: :first,
            as: :radio_buttons , label: 'Choose language' %>

Is there a way to transform the SecurityUser::LANGUAGE_DETAILS hash into new one in this context or I should create the hash on hand in the model?

Upvotes: 2

Views: 209

Answers (1)

sawa
sawa

Reputation: 168121

You can do it like this:

Hash[LANGUAGE_DETAILS.map{|k, h| [k, h[:Name]]}]

Upvotes: 2

Related Questions