C. Passmore
C. Passmore

Reputation: 15

options for select outputting blank values and label values

I'm trying to populate a select tag from an array of hashes. When I use options_for_select, it's outputting the correct number of options, but value label and option text is not there.

[{:name=>"Label", :id=>"326613406", :thumb=>"https://something/whatever"},another hash]

formatted_options = options_for_select(select_options.collect { |f| [ f['name'], f['id'], {'data-thumb' => f['thumb']} ] })

return select_tag "#{object.class.to_s.downcase + '[content][' + field.name + ']'}", formatted_options, {class: "image-support-select"}

I would expect that the value and text of the option elements would show. Also, I'd like to include a data attribute for each option tag.

Upvotes: 0

Views: 157

Answers (1)

asimhashmi
asimhashmi

Reputation: 4378

You are defining your hash keys as symbol but in your loop you are accessing them as string.

Change your loop like this:

formatted_options = options_for_select(select_options.collect { |f| [ f[:name], f[:id], {'data-thumb' => f[:thumb]} ] })

If you want to access your hash both by using symbol keys and string keys, you can call with_indifferent_access method of hash

In that case you need to change your hash like this:

[{:name=>"Label", :id=>"326613406", :thumb=>"https://something/whatever"}.with_indifferent_access]

Upvotes: 1

Related Questions