I have an enumeration in my model as follows:
enum construction_type: {
brick_block: "Brick/Block",
concrete_slab: "Concrete/Slab",
wood_steel: "Light Framed Wood/Steel",
timber_steel: "Heavy Framed Timber/Steel"
}
In a form, I use this code to grab the values of the enum to put into a dropdown:
<%= form.label(:construction_type, class: "form-label") %>
<% options = options_for_select(Site.construction_types.map {|key, value| [value, Site.construction_types.key(value)]}, form.object.construction_type) %>
<%= form.select(:construction_type, options, include_blank: true) %>
While the statement in options_for_select seems like overkill when Site.construction_types.values yields the same options, the field only remains populated after an invalid submit on the form when using the mapped method.
One solution I've found is to hardcode the strings into the form as follows:
<%= form.label(:construction_type, class: "form-label") %>
<%= form.select(:construction_type, ["Brick/Block", "Concrete/Slab", "Light Framed Wood/Steel", "Heavy Framed Timber/Steel"], include_blank: true) %>
However, I would like to avoid this solution as I have a second form used to edit the information initialized in this one where I would have to duplicate the code. An enumeration in the model seems like the best way to keep track of these values.
My database populates as I would like with the values from the enum, but on a page where I am trying to display the information from the form, the keys appear instead.
<li> <strong> <%= t(".construction_type") %> </strong> <%=@site.construction_type if @site.construction_type %> </li>
Using the enumerated version, the code above yields the following: Construction Type: brick_block
As opposed to what I want: Construction Type: Brick/Block
Is there a way to fix this using the enumeration method?