I have a class in Model which has a hash defined.
{:"manager" => 1, :"staff" => 2}.freeze
in my form partial.
<li class="field">
<%= f.label :position_type %><br />
<%= f.select :position_type, @person.position_types, {:include_blank => true} %>
</li>
So, when I press submit button in the show.html.erb I could see only the values of the selected values but not keys which is what I intended to have.
Q: How can I tweak it so that it will show the key rather than values. I know it has much to do with select tag rather than hash.
PS:
-
I already used
arrayand its doing what I want. -
hash.invertwhich does show thekeysin the show.html.erb but not in new/edit.html.erb and it being that way is not of that much help as user who tries to fill the form cannot memorise the keys => values to arrive on the keys as output.
I am just curious about how does the select in Rails takes the HTML name and value attirb. I am assuming that since hash already has value for each associated key, rails implicitly takes the key's value as HTML value attrib when form is submitted.
According to the documentation http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html
the names and ids have to be in a nested array. To create that from your map you can use: