我从下面的符号中添加了getter和setter:
class User < ActiveRecord::Base
GENDER = { male: 2, female: 1}
def gender
GENDER.key(read_attribute(:gender))
end
def gender=(g)
write_attribute(:gender, GENDER[g.to_sym])
end
end
如何添加选择字段以选择性别?我想从GENDER
动态获取选项。
= simple_form_for [:member, @member] do |f|
= f.input :gender
答案 0 :(得分:2)
您需要使用馆藏,请参阅此处的文档:https://github.com/plataformatec/simple_form#collections
<%= f.input :gender, collection: User::GENDER.keys %>