我有一個名爲Transaction的嵌套表單,其中包含一個名爲Banking Information Form的子對象。當父母(交易)transaction_mechanism字段設置爲「檢查」時,不應觸發銀行信息驗證。出於某種原因,當我將表單中的transaction_mechanism設置爲「check」時,它仍然調用銀行信息表單驗證。在bank_information_form模型中,我有一個名爲cheque_transaction的方法?如果transaction_mechanism是「check」,應該防止觸發驗證,但是我收到一個錯誤。在條件驗證中沒有給出塊(yield)rails錯誤
錯誤
no block given (yield)
transaction.rb
class Transaction < ActiveRecord::Base
# Require packages
require 'time'
# Associations
belongs_to :user
has_one :banking_information_form
# Nested attributes
accepts_nested_attributes_for :banking_information_form
# Validations
validates_associated :banking_information_form
validate :check_debit_or_credit
validates :transaction_mechanism, :transaction_kind, :debit, :salesforce_id, presence: true
validate :debit, :credit,numericality: { only_integer: true }
end
banking_information_form.rb
class BankingInformationForm < ActiveRecord::Base
# Associations
# Belongs to :transaction
belongs_to :owner, foreign_key: "transaction_id", class_name: "Transaction"
# Validations
validates :financial_institution, :transit_number, :bank_account_number, presence: true, :unless => :cheque_transaction?
validates :transit_number, length: {is: 5,
too_short: "Your transit number is 5 digits long.",
too_long: "Your transit number is 5 digits long."}
validates :bank_account_number, length: {minimum: 4, maximum: 12,
too_short: "Your bank account number will be between 4 and 12 digits long.",
too_long: "Your bank account number will be between 4 and 12 digits long."}
private
def cheque_transaction?
self.transaction.transaction_mechanism == "Cheque"
end
結束
1 「的時候,父母的(交易)transaction_mechanism字段設置爲銀行業信息驗證應該只觸發」檢查「 2.由於某種原因,當我在表單中設置transaction_mechanism爲」check「時,它仍然調用銀行信息表單驗證「 因此,transaction_mechanism設置爲'檢查'類型,所以它被調用?那麼問題是什麼? – Ajay 2015-01-09 23:38:58
@Ajay謝謝你指出。修復這個問題是有道理的! – Questifer 2015-01-09 23:44:31