NoMethodError: DateTime对象的未定义方法“getlocal”

时间:2021-07-08 23:54:31

When a user logs into my site, Devise tries to update the field in the user model current_sign_in_at with the time. But it's producing this error:

当用户登录到我的站点时,设计尝试在用户模型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

I've found a couple of resources where people have mentioned this problem. There's an issue on Github with no answer. This page has a solution which works in my development environment, because it involves editing /lib/active_support/time_with_zone.rb, but I need a solution that will also work in my production environment on Heroku. Is there a way I can use my edited version of /lib/active_support/time_with_zone.rb on Heroku? Or is there a better way to deal with the problem?

我找到了一些人们提到这个问题的资源。Github上有个问题没有答案。这个页面有一个在我的开发环境中工作的解决方案,因为它涉及编辑/lib/active_support/time_with_zone。rb,但是我需要一个在Heroku的生产环境中也能工作的解决方案。我是否可以使用我的编辑版本/lib/active_support/time_with_zone。rb Heroku吗?还是有更好的方法来解决这个问题?

Here's what I'm using:

这就是我使用:

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

Thanks for reading.

感谢你的阅读。

2 个解决方案

#1


2  

 irb> Time.now.getlocal
 => Mon Nov 08 15:04:05 +0200 2010 

but

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

So, I assume, you need to convert your DateTime object to Time

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

Updated

更新

You can use Ruby mixin technique, something like

您可以使用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!" 

#2


2  

I know this question was 2 years ago, but I think I found the clearer explanation about why it behave that way, found on this question. Just for anyone who probably need more explanations like me.

我知道这个问题是两年前的事了,但我认为我找到了更清晰的解释,解释了为什么它会这样,在这个问题上。只是给那些像我一样需要更多解释的人。

#1


2  

 irb> Time.now.getlocal
 => Mon Nov 08 15:04:05 +0200 2010 

but

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

So, I assume, you need to convert your DateTime object to Time

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

Updated

更新

You can use Ruby mixin technique, something like

您可以使用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!" 

#2


2  

I know this question was 2 years ago, but I think I found the clearer explanation about why it behave that way, found on this question. Just for anyone who probably need more explanations like me.

我知道这个问题是两年前的事了,但我认为我找到了更清晰的解释,解释了为什么它会这样,在这个问题上。只是给那些像我一样需要更多解释的人。