我正在使用模型字段的mongoid并给出验证:
field :status, type:String, :default=>'Active'
validates :status, :inclusion=>{:in=>%w(Active, Done, Canceled, Merged)}, :allow_nil=>true, :allow_blank=>true
在表单中,我没有status字段,因此它应该不是POST,因此它在创建时是零:
= 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
为什么我仍然会收到此错误?
提前致谢!
答案 0 :(得分:3)
这是你的问题
%w(Active, Done, Canceled, Merged)
转换为
["Active,", "Done,", "Canceled,", "Merged"]
解决方案是删除逗号
%w(Active Done Canceled Merged)