I’ve just started with my first real rails app.
I have a predefined set of salutations (“Herr”, “Frau, “Herr und Frau”) for my customer model.
The app has to deal with an export to a csv file where “Herr” has to be the integer 1, “Frau” 2 and “Herr und Frau” = 3.
Therefore I thought it would be clever to save that salutation as an integer and simply add an salutation_readable method to my customer model like this:
def salutation_readable
case salutation
when 1
"Herr"
when 2
"Frau"
when 3
"Herr und Frau"
end
end
to display the salutation in my views.
In the customer form partial I added:
<%= f.select(:salutation, options_for_select([['Herr', 1], ['Frau', 2], ['Herr und Frau', 3]])) %>
Everything works as expected, except the customer edit view where the preselected value is always the first entry and not my saved one.
options_for_select takes a second parameter as the default selected option.