1

我正在使用带有模型字段的 mongoid 并给出验证:

field :status, type:String, :default=>'Active'
validates :status, :inclusion=>{:in=>%w(Active, Done, Canceled, Merged)}, :allow_nil=>true, :allow_blank=>true

在表单中,我没有状态字段,所以它应该不是 POST-ed,因此它在创建时为零:

= simple_form_for([@user, @task], :html => {:class=>'form-horizontal',:'data-type'=>'html'}) do |f|
  - if @task.errors.any?
    .error_explanation
      .alert.alert-error
        The form contains
        = pluralize(@task.errors.count, 'error')
      %ul
        - @task.errors.full_messages.each do |msg|
          %li=msg
  .form-inputs
    = f.error_notification
    = f.association :project, :collection => current_user.projects.all
    = f.input :description, :as => :text, :input_html => {:rows => 5}
    = f.input :priority, :as=>:radio_buttons, :collection=>1..4, :item_wrapper_class=>'inline'
    = f.input :due_date

    .control_group.select.optional
      = f.label :assigned_to, :class=>'select optional control-label', :for => 'assigned_to_id'
      .controls
        = f.collection_select :assigned_to_id, User.all, :id, :username, :class => 'select optional'

  .form-actions
    = f.button :submit, :class => 'form-button btn-primary', 'data-loading-text' => 'Submitting...'

但是,尽管设置了默认值“Active”,我仍然得到这个,这显然是在为包含验证提供的数组中:

Status is not included in the list

为什么我仍然收到此错误?

提前致谢!

4

1 回答 1

3

这是你的问题

%w(Active, Done, Canceled, Merged)

这转化为

["Active,", "Done,", "Canceled,", "Merged"]

解决方案是删除逗号

%w(Active Done Canceled Merged)
于 2013-03-04T00:29:50.927 回答