I have been searching for hours to solve a problem in one of my forms. One of my models has an enum field with some options. e.g. enum(‘a’, ‘b’, ‘c’)
When i edited an entry of this model class i noticed that my listbox didn’t have the correct option chosen.
e.g.
<%= select('myobject', 'type', ["a","b","c"]) %>
didn’t work
<%= select('myobject', 'type', ["a","b","c"], {:selected => @myobject.type}) %>
didn’t work either
After many hours and having to develop a helper select function of my own, i found out that this works:
<%= select('myobject', 'type', ["a","b","c"], {:selected => @myobject.type.to_s}) %>
For some reason rails doesn’t recognize the enum field’s value as a string and the comparisons don’t take place correctly. After casting it to string it worked 🙂
That’s all for now. Bye.