我試圖在名爲「birthday」的文本字段中輸入日期'03/20/1985',並將其插入到列類型爲「date」的數據庫字段中。Rails慢性寶石沒有按預期驗證
當我輸入10/20/1985
時,出現錯誤「生日無效」,但是當我輸入20/10/1985
時,它工作得很好。
從我一直在閱讀的所有文檔中,chronic應該將mm/dd/yyyy解析爲'10/20/1985',但它似乎將其解析爲dd/mm/yyyy。
我該如何解析日期爲mm/dd/yyyy?
/models/user.rb
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :authentication_keys => [:login]
# Virtual attribute for authenticating by either username or email
# This is in addition to a real persisted field like 'username'
attr_accessor :login
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me, :username, :login, :first_name, :last_name, :home_phone, :cell_phone, :work_phone, :birthday, :home_address, :work_address, :position, :company
validate :birthday_is_date
validate :position, :presence => true
require 'chronic'
# validate the birthday format
def birthday_is_date
errors.add(:birthday, "is invalid") unless Chronic.parse(birthday)
end
# validates email or username when logging in
def self.find_first_by_auth_conditions(warden_conditions)
conditions = warden_conditions.dup
if login = conditions.delete(:login)
where(conditions).where(["lower(username) = :value OR lower(email) = :value", { :value => login.downcase }]).first
else
where(conditions).first
end
end
end
數據庫中的值是什麼樣的? –
如果我使用'20/10/1985'分貝顯示'2012-10-20',但如果我使用'10/20/1985',沒有任何更新到數據庫,我收到錯誤消息。 – Catfish
不知道是否重要,但這是一個設計模型 – Catfish