0
我在我的模型內部創建了一個簡單驗證,檢查日期(to_date
)是否在當前日期之前。Rails 4的strptime錯誤
用戶提交to_date
的格式是%m/%d/%Y
或者例如03/15/2016
。
我的問題是,鐵軌扔我invalid date
錯誤,雖然我有三重檢查,日期是在正確的格式。
這裏是我的驗證:
validate :date_not_before_now
def date_not_before_now
if Date.strptime(self.to_date, '%m/%d/%Y').strftime("%Y-%m-%d") < Time.now.localtime.strftime("%Y-%m-%d")
errors.add(:dates, "should not be in the past.")
end
末
任何弄清楚爲什麼這不工作,將不勝感激幫助。
感謝您的建議,但on_or_before不會在我的情況下工作,因爲'to_date'是一個字符串 - 而不是日期時間。 – Kathan