简体   繁体   English

accept_nested_attributes_for + reject_if +未保存的子对象的显示错误

[英]accept_nested_attributes_for + reject_if + display error for unsaved child objects

I have a 'Parent' has-many 'Children' relationship among 2 models. 我有2个模型之间的“父母”有很多“孩子”关系。 im using accept_nested_attributes_for n the 'reject_if' feature. 我在'reject_if'功能中使用accept_nested_attributes_for。 i want to display the a error message as "2 Child objects could not be saved." 我想显示一条错误消息为“无法保存2个子对象。” when the parent model is saved. 保存父模型时。

You can use validates_associated to bring up validation errors if any of the children are invalid. 如果任何子级无效,则可以使用validates_associated引发验证错误。 I think if you are using reject_if it won't actually build the child object, so you might need to remove the reject_if condition for the validation errors on the child to be shown. 我认为,如果您正在使用reject_if则它实际上不会构建子对象,因此您可能需要删除reject_if条件,以显示子项上的验证错误。

Class Parent
  has_many :children
  validates_associated :children
end

class Child
  belongs_to :parent
end

More info here: http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html 此处提供更多信息: http : //api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM