Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 时间范围方法在Rails 4/Ruby 2应用程序中不起作用_Ruby On Rails_Ruby - Fatal编程技术网

Ruby on rails 时间范围方法在Rails 4/Ruby 2应用程序中不起作用

Ruby on rails 时间范围方法在Rails 4/Ruby 2应用程序中不起作用,ruby-on-rails,ruby,Ruby On Rails,Ruby,我需要帮助解决Rails 4/Ruby 2.0应用程序中的时间范围问题。在我的约会模型中,我希望阻止用户在工作时间以外(即上午6:30到晚上9:00)进行约会。该模型在名为“约会\ U日期”的列中跟踪约会时间,该列为日期时间数据类型 我试图通过在app/validators目录中放置以下方法来解决此问题: class DuringBusinessHoursValidator < ActiveModel::EachValidator def validate_each(record, at

我需要帮助解决Rails 4/Ruby 2.0应用程序中的时间范围问题。在我的约会模型中,我希望阻止用户在工作时间以外(即上午6:30到晚上9:00)进行约会。该模型在名为“约会\ U日期”的列中跟踪约会时间,该列为日期时间数据类型

我试图通过在app/validators目录中放置以下方法来解决此问题:

class DuringBusinessHoursValidator < ActiveModel::EachValidator

def validate_each(record, attribute, value)    
unless value.present? && during_business_hours(value)
record.errors[attribute] << 'must be during business hours (6:30 am - 9:00 pm)'
end
end

def during_business_hours(time)
# from http://stackoverflow.com/q/10090962/525478    
Range.new(
Time.local(time.year, time.month, time.day, 6, 30),
Time.local(time.year, time.month, time.day, 21, 0)) === time
end
end
但是,Rails会抛出以下错误:

TypeError in AppointmentsController#update
can't iterate from Time
当我删除方法中的一个===符号时,Rails不会生成错误,但代码不能正确运行。使用==时,所有约会都会受到限制,即使是在上午6:30到晚上9:00之间进行的约会

我读了另一篇Stackoverflow文章,其中有人报告在Ruby 2.0中执行上述方法时出现问题。此人表示,他们通过“将范围端点转换为.to_i,然后与t.to_i进行比较”解决了问题。因此,我将第二种方法中的代码更改如下:

def during_business_hours(t)

t = Time.now

Range.new(
Time.local(t.year, t.month, t.day, 6, 30).to_i,
Time.local(t.year, t.month, t.day, 21).to_i
) === t.to_i
end
end
然而,这种改变并不奏效。它产生了相反的问题:Rails允许所有约会,而不考虑时间


对于如何进一步排除故障并解决问题,有什么想法或建议吗?我将感谢任何帮助

我会利用DateTime对象支持比较这一事实。例如

   Time.local(2014, 12, 31, 6, 30) > Time.local(2015, 1, 1, 6, 30)
   => false
因此,您可以在工作时间内重写方法

   # Check if time `t` is between 6:30 and 21:00 of `t`'s day, a.k.a. in between business hours.
   # If that's the case, return true, otherwise false.
   def during_business_hours(t)
     if t < Time.local(t.year, t.month, t.day, 6, 30) ||
        t > Time.local(t.year, t.month, t.day, 21)
        return false
     else
        return true
     end
   end

谢谢你,肯尼。我将尽早查看您建议的代码。另外,感谢您对我的代码可读性的反馈。我正在学习用Rails和Ruby编写代码,因此我非常感谢您的反馈。你的代码更清晰。我希望它能解决我的问题。我试过之后再跟进。谢谢不客气,有很多方法可以解决问题。Ruby是一种可以以非常可读的方式解决问题的语言。如果你想聊天,可以在推特上给我打电话。我对我的应用程序进行了更改。没有错误,但范围工作不正常。用户不得在晚上9:01到早上6:29之间预约。但是,该守则限制预约时间为上午12:00至下午2:29。目前,用户最早可以预约的时间是下午2:30,但应该是早上6:30。允许用户预约的最晚时间是晚上11:59,但应该是晚上8:59。约会的开始时间为8小时,结束时间为3小时。时间.now.to.s显示正确的时间。有什么不对劲吗?忽略我上一篇文章。我通过将本地时区放在application.rb文件的config.time_zone部分解决了时间问题。运行rake time:zones:all生成了一个完整的时区列表。现在一切正常。谢谢你的帮助,肯尼!我很高兴你发现了原来的错误!看起来你原来的代码还能用吗?
   # Check if time `t` is between 6:30 and 21:00 of `t`'s day, a.k.a. in between business hours.
   # If that's the case, return true, otherwise false.
   def during_business_hours(t)
     if t < Time.local(t.year, t.month, t.day, 6, 30) ||
        t > Time.local(t.year, t.month, t.day, 21)
        return false
     else
        return true
     end
   end
def validate_each [snip]

private

def during_business_hours(t)
  [snip]