Rails formtastic - whole enumerated field as a drop-down menu - '0' is not valid

I have an integer field 'fieldname'.

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

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

After selecting and sending, I get the error message "0" is not a valid field name.

It currently works by writing setters for integer fields using enum as follows:

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

Can you tell me the right way? I don't think writing this setter method is a good way.

+4
source share
1 answer

Add .keysto the collection definition:

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

, , , ActiveRecord .

+14

Source: https://habr.com/ru/post/1539269/


All Articles