Ruby on rails Rails 4.1.2-拒绝被忽略的嵌套属性结果
我有一个Rails表单,它有两种类型的嵌套属性:Pendencies和Address 但是,这两个字段不是必填字段,为空时应忽略:Ruby on rails Rails 4.1.2-拒绝被忽略的嵌套属性结果,ruby-on-rails,ruby-on-rails-4,nested-attributes,Ruby On Rails,Ruby On Rails 4,Nested Attributes,我有一个Rails表单,它有两种类型的嵌套属性:Pendencies和Address 但是,这两个字段不是必填字段,为空时应忽略: accepts_nested_attributes_for :pendencies, allow_destroy: true, reject_if: proc { |attributes| attributes['status'].blank? && attributes['description'].blank? && (
accepts_nested_attributes_for :pendencies, allow_destroy: true,
reject_if: proc { |attributes|
attributes['status'].blank? &&
attributes['description'].blank? &&
(attributes['pendency_type'].blank? || attributes['pendency_type'] == 0) }
accepts_nested_attributes_for :address, allow_destroy: true,
reject_if: proc { |attributes|
attributes['zipcode'].blank? &&
attributes['street'].blank? &&
(attributes['number'].blank? || attributes['number'] <= 0) &&
attributes['neighbour'].blank? &&
attributes['city'].blank? &&
attributes['state'].blank? }
对于消息中的PT-BR表示抱歉,它所说的只是长度较短且字段无效
我想知道如何忽略这些嵌套属性,以便Rails不会试图保存它们
谢谢。我通常不使用accepts\u nested\u attributes\u,因为它在最佳实践中不受欢迎,但在我看来,您需要为嵌套模型中的验证添加条件,并在您认为应该跳过的情况下跳过父模型中的条件。@Nathan那么您认为我也应该添加IF逻辑来忽略嵌套模型上的验证吗?值得一试。如果接受嵌套的属性并不是真正的最佳实践,那是什么呢?现在人们倾向于表单对象模式。它更加面向对象,而且总体上更易于管理@Nathan我了解表单对象模式,非常有用,谢谢。但是,它并不是我正在寻找的答案。答案是,在嵌套模型中的验证器上考虑条件逻辑。表单对象模式是对您的后续问题的响应。
@messages=
{:"pendencies.status"=>["não pode ficar em branco"],
:"pendencies.description"=>["não pode ficar em branco"],
:"pendencies.pendency_type"=>["não pode ficar em branco"],
:"address.zipcode"=>["não pode ficar em branco", "não é válido", "não possui o tamanho esperado (8 caracteres)"],
:"address.street"=>["não pode ficar em branco", "é muito curto (mínimo: 3 caracteres)"],
:"address.number"=>["não pode ficar em branco", "não é um número", "é muito curto (mínimo: 1 caracteres)"],
:"address.city"=>["não pode ficar em branco", "é muito curto (mínimo: 2 caracteres)"],
:"address.state"=>["não pode ficar em branco", "é muito curto (mínimo: 2 caracteres)"]}>