2016-01-28 40 views
5

後我關於Ruby 2.2.1並active_support 4.2安裝,所以我想用未定義的方法`zone`的時間:類需要active_support/time_with_zone

Time.zone.parse('2007-02-10 15:30:45') 

如下所述:http://api.rubyonrails.org/classes/ActiveSupport/TimeWithZone.html

但即使在要求active_supportactive_support/time_with_zone之後,我似乎仍然不能使用Time.zone。觀察:

2.2.1 :002 > require "active_support" 
=> true 
2.2.1 :003 > Time.zone 
NoMethodError: undefined method `zone' for Time:Class 
2.2.1 :004 > require "active_support/time_with_zone" 
=> true 
2.2.1 :005 > Time.zone 
NoMethodError: undefined method `zone' for Time:Class 

發生了什麼事?

回答

2

Timezone類方法實際上是active_support/core_ext/time/zones。您可以要求類,如果你真的想用Time.zone但更好的辦法可能要求active_support/all

建議的解決方案

require "active_support/all" 

如果你想檢查出active_support看看源代碼堡github repo

+0

謝謝!這就說得通了! – User314159

1

嘗試

require 'active_support/all' 

,而不是

require "active_support" 
+0

的感謝!這就說得通了! – User314159

3

active_support/time_with_zone提供了ActiveSupport::TimeWithZone類,但它並沒有擴展核心Time類。

您可以要求active_support/time代替:

require 'active_support' 
require 'active_support/time' 

Time.zone = 'Eastern Time (US & Canada)' #=> "Eastern Time (US & Canada)" 
Time.zone.parse('2007-02-10 15:30:45') #=> Sat, 10 Feb 2007 15:30:45 EST -05:00 
+0

謝謝!這就說得通了! – User314159

+0

對於那些事後看這個問題的人,我相信這是比我上面接受的答案更好的解決方案。感謝您發佈Stefan。 – Genzume

相關問題