我在我的一个 Rails 模型中有一个简单的验证,但它似乎并没有像我想要的那样触发错误。
before_save :check_future_date
private
def check_future_date
puts "=============================================================="
puts self.article.date
puts Date.today
if self.article.date <= Date.today
puts "error!!!!!!!!!!!!!!!!!"
errors[:base] << "Sorry, you must post at least a day in advance"
end
end
我知道正在触发逻辑,因为在控制台中,我看到:
==============================================================
2013-04-06
2013-04-29
error!!!!!!!!!!!!!!!!!
但是,记录仍然被保存,并且没有显示错误信息。我也试过:
errors.add(:base, 'Sorry, you must post at least a day in advance')