【问题标题】:NoMethodError: undefined method `getlocal' for DateTime objectNoMethodError:DateTime 对象的未定义方法“getlocal”
【发布时间】:2010-11-08 12:10:37
【问题描述】:

当用户登录我的站点时,Devise 会尝试使用时间更新用户模型 current_sign_in_at 中的字段。但它产生了这个错误:

NoMethodError: undefined method `getlocal' for Mon, 08 Nov 2010 03:11:01 +0000:DateTime
[GEM_ROOT]/gems/activesupport-3.0.1/lib/active_support/time_with_zone.rb:75

我找到了一些人们提到这个问题的资源。有一个issue on Github 没有答案。 This page 有一个适用于我的开发环境的解决方案,因为它涉及编辑/lib/active_support/time_with_zone.rb,但我需要一个也适用于我在 Heroku 上的生产环境中的解决方案。有没有办法可以在 Heroku 上使用我编辑的 /lib/active_support/time_with_zone.rb 版本?或者有没有更好的方法来处理这个问题?

这是我正在使用的:

Rails 3.0.1
ruby 1.9.2p0 (2010-08-18 revision 29036)

感谢阅读。

【问题讨论】:

    标签: ruby-on-rails datetime ruby-on-rails-3 heroku


    【解决方案1】:
     irb> Time.now.getlocal
     => Mon Nov 08 15:04:05 +0200 2010 
    

    但是

    irb>DateTime.now.getlocal
    NoMethodError: undefined method `getlocal' for Mon, 08 Nov 2010 15:05:16 +0200:DateTime
    from (irb):17
    

    因此,我假设您需要将 DateTime 对象转换为时间

    更新

    你可以使用 Ruby mixin 技术,比如

    irb > module DateTimePatch
    irb ?>  def get_local
    irb ?>    "works!"
    irb ?>    end
    irb ?>  end
     => nil 
    irb > DateTime.send(:include, DateTimePatch)
     => DateTime 
    irb2 > DateTime.now.get_local
     => "works!" 
    

    【讨论】:

    • 感谢您的回答。这基本上就是我在开发环境中所做的,通过更改 /lib/active_support/time_with_zone.rb 中的相关方法以使用 Time 对象。但问题是我不知道如何在生产环境中使用此修复程序,因为我不知道如何编辑 Heroku 中使用的 Ruby 库。
    • @ben 你可以使用 Ruby mixin 技术,比如我在上面编辑了我的答案
    【解决方案2】:

    我知道这个问题是 2 年前的问题,但我想我在 question 上找到了关于它为什么会这样表现的更清晰的解释。仅适用于像我这样可能需要更多解释的人。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-29
      • 1970-01-01
      • 2016-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多