如何在HTML select元素中使用活动枚举枚举?

时间:2014-07-27 13:10:12

标签: ruby-on-rails ruby active-enum

我在我的Rails 4应用程序中使用active_enum gem。 https://github.com/adzap/active_enum

gem 'active_enum'

在我的模型中,我有一个枚举:

class Meeting < ActiveRecord::Base
  enumerate :participant_type do
    value 0 => 'Juniors'
    value 1 => 'Senior'
    value 2 => 'Administration'
  end
end

如何在表单选择元素中使用这些值?

我尝试过以下操作,但收到运行时错误:

= f.select :participant_type, Loan.participant_type

undefined method `participant_type' for #<Class:0x007f8803581050>

1 个答案:

答案 0 :(得分:1)

将参与者类型作为一个单独的类,扩展ActiveEnum :: Base并尝试.to_select方法。

= f.select :participant_type, ParticipantType.to_select, required: true

如果是Active Record Enum,请尝试以下操作:

docs中所述,它是复数participant_types

= f.select :participant_type, Meeting.participant_types, required: true