如何使用符号getter在SimpleForm中添加选择字段?

时间:2013-11-01 10:56:58

标签: ruby-on-rails ruby simple-form

我从下面的符号中添加了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

1 个答案:

答案 0 :(得分:2)

您需要使用馆藏,请参阅此处的文档:https://github.com/plataformatec/simple_form#collections

 <%= f.input :gender, collection: User::GENDER.keys %>