【问题标题】:Rails: undefined method `Europe/Rome' for Time:ClassRails:时间:类的未定义方法“欧洲/罗马”
【发布时间】:2014-02-09 18:50:33
【问题描述】:

创建班次时出现此错误...

ActiveRecord::MultiparameterAssignmentErrors (2 error(s) on assignment of multiparameter attributes [error on assignment [8, 0, 1970, 1, 1] to inizio (undefined method `Europe/Rome' for Time:Class),error on assignment [16, 0, 1970, 1, 1] to fine (undefined method `Europe/Rome' for Time:Class)]):
  app/controllers/shifts_controller.rb:43:in `create'

在 application.rb 我有

config.time_zone = 'Europe/Rome'
config.active_record.default_timezone = 'Europe/Rome'

模型是

class Shift < ActiveRecord::Base

  after_initialize do
    self.stato ||= true
    self.inizio ||= Time.zone.now
    self.fine ||= Time.zone.now
  end

end

控制器是: http://nopaste.info/3268dc795c.html

迁移是: http://nopaste.info/eb674af117.html

【问题讨论】:

标签: ruby time ruby-on-rails-4 timezone


【解决方案1】:

问题是active_record.default_timezone 只接受:local:utc,因此您必须以这种方式更改设置:

config.time_zone = 'Rome'
config.active_record.default_timezone = :local

Dopodiché ti suggerisco di scrivere semper in inglese mentre programmi per avere coerenza all'interno del codice。
顺便说一句,我建议您在编码时始终输入英文以提高一致性

查看此GIST,其中详细说明了为什么要使用:local
另请注意这篇文章的第二个答案,这是正确的一个:https://stackoverflow.com/a/12127232/312907

请注意,这是'Rome',而不是'Europe/Rome'

【讨论】:

    【解决方案2】:

    对于欧洲/罗马,rails 实际上已将其映射到“罗马”,因此您应该能够这样做:

    config.time_zone = 'Rome'
    config.active_record.default_timezone = 'Rome'
    

    您可以在ActiveSupport::TimeZone 的文档中找到所有时区映射

    【讨论】:

    • 我只需添加“欧洲/罗马”或其他首都的等价物只是时区信息的标识符字符串(TZInfo::TimeZoneProxy 类的tzinfo)。
    猜你喜欢
    • 2016-07-11
    • 1970-01-01
    • 2021-10-22
    • 1970-01-01
    • 2020-09-01
    • 2021-11-15
    • 2020-03-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多