4

我正在尝试将日期“03/20/1985”输入到名为“生日”的文本字段中,并将其插入列类型为“日期”的数据库字段中。

当我输入时10/20/1985,我收到错误“生日无效”,但是当我输入时20/10/1985,它工作得很好。

从我一直在阅读的所有文档中,慢性应将“10/20/1985”解析为 mm/dd/yyyy,但似乎将其解析为 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
4

2 回答 2

3

如果值作为日期存储在数据库中,Rails 将在赋值时将值从字符串强制转换为 Ruby 日期。我认为它可能使用内置Date.parse方法(docs):

Date.parse "2012-10-20"
# => #<Date 2012-10-20 ...>

Date.parse "20-10-2012"
# => #<Date 2012-10-20 ...>

Date.parse "10-20-2012"
# => ArgumentError: invalid date

在这种情况下,您希望避免强制并获得原始字符串以使用 Chronic 进行解析。这是虚拟属性的理想用例。有几种方法可以做到这一点,这样的事情应该让你开始

class User < ActiveRecord::Base
  validate :birthday_is_date

  # an explicit implementation
  def birthday_string
    if @birthday_string
      @birthday_string
    elsif birthday.present?
      birthday.strftime("%d-%m-%Y")
    else
      ""
    end
  end

  # a shorter implementation
  def birthday_string
    @birthday_string || birthday.try(:strftime, "%d-%m-%Y")
  end

  def birthday_string=(value)
    @birthday_string = value
    self.birthday = parse_birthday
  end

  private

  def birthday_is_date
    errors.add(:birthday_string, "is invalid") unless parse_birthday
  end

  def parse_birthday
    Chronic.parse(birthday_string)
  end
end

然后birthday_string在您的表单中使用,而不是birthday.

于 2012-12-27T13:50:28.260 回答
1

两者都对我有用,也许您需要更新。

还:

Chronic.parse '2/10/1985'
#=> 1985-02-10 12:00:00 +0800
Chronic.parse '2/10/1985', :endian_precedence => :little
#=> 1985-10-02 12:00:00 +0800
于 2012-11-09T04:55:21.423 回答