【发布时间】:2011-07-26 23:05:27
【问题描述】:
我有一个这样定义的联系信息类:
class ContactInfo
include Mongoid::Document
validates_presence_of :name, :message => ' cannot be blank'
field :name, :type => String
field :address, :type => String
field :city, :type => String
field :state, :type => String
field :zip, :type => String
field :country, :type => String
embedded_in :user
end
此联系信息类作为嵌套属性嵌入到我的用户类中:
class PortalUser
include Mongoid::Document
accepts_nested_attributes_for :contact_info
end
当我尝试保存没有姓名的用户时,我收到如下错误消息:
联系方式无效
但是,这对最终用户来说不是很有用,因为他或她不知道哪些联系信息是无效的。 REAL 消息应该是“名称不能为空”。但是,此错误不会向上传播。有没有办法在 user.errors 中获取“名称不能为空”消息而不是“联系信息无效”错误消息?
谢谢
【问题讨论】:
-
Mongo 内置了这个,带有 validates_associated。见stackoverflow.com/questions/5078661/…
-
validates_associated 仅验证关联模型,但仍会吐出无用的错误消息。
标签: validation nested mongoid propagation