nested_form:孩子在验证错误时消失

时间:2016-05-03 09:02:09

标签: ruby-on-rails rails-activerecord

我能够验证子记录,但是在提交表单后,有错误的记录会消失。

class Deduction < ActiveRecord::Base
    belongs_to :parent_source, polymorphic: true, inverse_of: :deductions
    belongs_to :debt, polymorphic: true, inverse_of: :deductions
    validates :payee_id, presence: true
    validates :debt_id, presence: true
    validates_uniqueness_of :debt_id, scope: :parent_source
end


class Voucher < ActiveRecord::Base
    has_many :deductions, dependent: :destroy, as: :parent_source, inverse_of: :parent_source
    accepts_nested_attributes_for :deductions, allow_destroy: true
end

1 个答案:

答案 0 :(得分:0)

不确定您的控制器是什么样的,但如果保存失败,请尝试将以下行添加到控制器的create操作中:

@ voucher.deductions.build除非@ voucher.errors.any?