2
我的自定義驗證器出現問題。我有一個模型Price
,它看起來像這樣:Rails 4自定義驗證
class Price < ActiveRecord::Base
belongs_to :car
validates :from_days, :to_days, :netto_price, presence: true, numericality: true
validate :days_range_validation
private
def days_range_validation
unless to_days > from_days
errors[:to_days] << I18n.t('price.must_be_greater')
end
end
end
而問題是,當我離開to_days
和from_days
空白在我的形式,我得到以下錯誤:
undefined method `>=' for nil:NilClass
而且現在的目標是僅當from_days
和to_days
存在時才使用此驗證程序,但我不知道該怎麼做。有任何想法嗎?
這是偉大的答案。 –
@MateuszUrbański謝謝。你介意將答案標記爲接受嗎? –