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

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

概述我正在尝试将日期’03 / 20/1985’输入到名为“birthday”的文本字段中,并将其插入到具有“date”列类型的数据库字段中. 当我输入1985年10月20日,我收到错误“生日无效”,但当我输入20/10/1985,它工作正常. 从我读过的所有文档中,慢性应该将’10 / 20/1985’解析为mm / dd / yyyy,但似乎它正在解析它为dd / mm / yyyy. 我怎样才能 我正在尝试将日期’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  endend
解决方法 如果该值作为日期存储在数据库中,则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)  endend

然后在表单中使用birthday_string,而不是生日.

总结

以上是内存溢出为你收集整理的ruby-on-rails – Rails慢性宝石未按预期验证全部内容,希望文章能够帮你解决ruby-on-rails – Rails慢性宝石未按预期验证所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1279524.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-09
下一篇2022-06-09

发表评论

登录后才能评论

评论列表(0条)

    保存