我有一个带有start_time
时间戳的 Rails 模型,这显然应该在将来,这是我的验证应该确保的。
约会.rb
validate :start_time_in_future, :on => :create
private
def start_time_in_future
errors.add(:base, 'Start time must be in the future') unless self.start_time > Time.now
end
约会控制器.rb
around_filter :start_time_in_future
def create
@appointment = Appointment.create(foo_params)
redirect_to @appointment
end
private
def start_time_in_future
begin
yield
rescue ActiveRecord::RecordInvalid => e
redirect_to request.referrer, :alert => e.message
end
end
这一切都很好,但它太过分了。我不能只有一个自定义验证失败并显示消息而不是异常吗?