Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails formtastic - integer field with enum as dropdown - '0' is not a valid

I have an integer field 'fieldname'.

enum drop: ['a', 'b']

f.input :fieldname, as: select, collection: Model.drops

After selecting and submitting, I am getting error '0' is not a valid fieldname.

Currently making it work by writing setters for integer fields using enum like this:

def fieldname=(value)
    self[:fieldname] = value.to_i
end

Can you tell me the proper way? I dont think writing this setter method is a good way.

like image 691
user1305989 Avatar asked May 05 '14 11:05

user1305989


1 Answers

Add .keys to the collection definition:

f.input :fieldname, as: select, collection: Model.drops.keys

This way the option's value will be the same string as the text and on saving ActiveRecord will convert it to integer.

like image 52
barvaz Avatar answered Nov 16 '22 00:11

barvaz