【问题标题】:Ruby + Mongoid: how to make a required field?Ruby + Mongoid:如何制作必填字段?
【发布时间】:2013-10-26 14:00:33
【问题描述】:

我使用 Mongoid 的 githead 版本(因为 Rails 4),并且我想制作一个字段必填文档:

class MyClass
  include Mongoid::Document
  field :name,              type: String, required: true

我有这个错误:

Problem: Invalid option :required provided for field :name. Summary: Mongoid requires that you only provide valid options on each field definition in order to prevent un...

我做错了什么?

【问题讨论】:

    标签: ruby-on-rails ruby mongoid ruby-on-rails-4


    【解决方案1】:

    你需要使用validates_presence_of,所以你的类应该是这样的:

    class MyClass
      include Mongoid::Document
      field :name, type: String
      validates_presence_of :name
    end
    

    有关 mongoid 验证的更多文档,您可以使用 http://mongoid.org/en/mongoid/docs/validation.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多