gpt4 book ai didi

ruby-on-rails-3 - 在 Rails 模型中一起验证日期和时间字段

转载 作者:行者123 更新时间:2023-12-05 08:02:05 24 4
gpt4 key购买 nike

编辑:如果下面的问题看起来有点“广泛”,总结是我只想在我的模型中组合一个日期字段和一个可选的时间字段,纯粹是为了使用日期验证器来验证它,但我当我传入一个字符串时,我的测试无法正确失败。


编辑 2:由于我仍在努力寻找一种将日期对象与时间对象连接起来以进行验证的方法,因此我想知道是否可以使用 验证时间值是否为时间对象is_a?(Time) (而不是无效字符串)但这似乎也不起作用,我想知道是不是因为 Time mention with this method (但我不太了解它是否会导致问题。

我也想知道 to_time method可以帮助我检查某物是否是 Time 对象,但我不确定如何检查。

希望有人可以建议如何将 Date 对象与 Time 对象结合起来进行验证,或者至少告诉我如何检查提交的时间值是一个 Time 对象,而不是无效的字符串或其他东西。


编辑 3:我刚刚根据我发现的另一个答案尝试了以下操作,但它仍然没有提示测试中的“无效”字符串:

validate :start_time_is_time?, :if => "start_time.present?"

def start_time_is_time?
unless start_time.is_a?(Time) || ((Time.parse(start_time) rescue ArgumentError) == ArgumentError)
errors.add(:start_time, 'must be a valid time')
end
end

是否有任何理由可以将字符串视为有效的 Time


我的观点类似于Trying to set a variable in before_validation but it isn't working具有使用日历选择日期并使用下拉菜单选择时间的 View 。我尝试将相同的方法应用于我的模型,但无法让它工作,因为我对 ruby​​ 有点陌生,并且在填写答案留下的空白时遇到了困难。

我的模型有 start_date 和 start_time 字段。

我需要合并这两个字段,这样我就可以将它们作为日期时间一起验证(使用 date_validator gem),而不必分别验证日期和时间,尽管它们将作为单独的字段进入数据库,除非有人可以说服我(尽管我已经分别与他们做了很多事情,所以真的不想改变这一点)。

必须提交日期,但时间字段是可选的。

从另一个问题看来,我需要一个 before_validation 方法,该方法可以根据需要将 2 个字段组合到一个虚拟字段中,然后我可以使用 date_validator gem 对其进行验证。

编辑:由于 RyanJM,原始错误已得到纠正,但我仍然无法正确通过测试。以下是当前的游戏状态。

现在,这是我的 Showtime 模型的基础知识:

class Showtime < ActiveRecord::Base

attr_accessible :start_date, :start_time

attr_accessor :starting_at, :starting_time

belongs_to :performance

before_validation :construct_starting_at

validates :start_date, :presence => true, :date => { :after_or_equal_to => Proc.new { Date.today } }
validates :starting_at, :date => { :after_or_equal_to => Proc.new { Time.now } }, :if => "start_date.present? && start_time.present?"

def construct_starting_at
if start_date.present? && start_time.present?
self.starting_time = self.start_time.strftime("%T")
self.starting_at = DateTime.strptime("#{start_date} #{starting_time}", "%F %T")
end
end

end

如果需要,这里是性能模型的基础/相关部分:

class Performance < ActiveRecord::Base

has_many :showtimes, :dependent => :delete_all
accepts_nested_attributes_for :showtimes, :allow_destroy => true, :reject_if => lambda { |a| a[:start_date].blank? }

end

这是失败的测试:

require 'spec_helper'

describe Showtime do

before(:each) do
@attr = {
:name => "Performance 1",
:showtimes_attributes => {
"0" => {
:start_date => Date.today+15.days,
:start_time => Time.now
}
}
}
end

it "should test that the start time is a valid time if it exists" do
@attr_invalid = {
"0" => {
:start_date => Date.today+15.days,
:start_time => "invalid"
}
}
invalid = Performance.create(@attr.merge(:showtimes_attributes => @attr_invalid))
invalid.should_not be_valid
end

end

希望我没有破坏上面代码中的任何内容,只是为了展示必要的部分。

最佳答案

我还没有使用 date_validator gem,但是 validates_timeliness这类事情做得很好,而且似乎也得到了更积极的维护。

关于ruby-on-rails-3 - 在 Rails 模型中一起验证日期和时间字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9955977/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com