加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – Rails慢性宝石未按预期验证

发布时间:2020-12-17 02:32:59 所属栏目:百科 来源:网络整理
导读:我正在尝试将日期’03 / 20/1985’输入到名为“birthday”的文本字段中,并将其插入到具有“date”列类型的数据库字段中. 当我输入1985年10月20日,我收到错误“生日无效”,但当我输入20/10/1985,它工作正常. 从我读过的所有文档中,慢性应该将’10 / 20/1985’
我正在尝试将日期’03 / 20/1985’输入到名为“birthday”的文本字段中,并将其插入到具有“date”列类型的数据库字段中.

当我输入1985年10月20日,我收到错误“生日无效”,但当我输入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

解决方法

如果该值作为日期存储在数据库中,则Rails会在赋值时将字符串中的值强制转换为Ruby Date.我认为它可能使用内置的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.这是virtual attributes的理想用例.有几种方法可以做到这一点,这样的事情应该让你开始

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,而不是生日.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读