2017-02-24 86 views

回答

2

我使用around_action改變我登臺環境的時間實現了這一與Timecop寶石。

module TimeTravelFilters 
    extend ActiveSupport::Concern 

    included do 
    if Time::Clock.travel_ok? 
     around_action :time_travel_for_request 
    end 
    end 

    def time_travel_for_request 
    time_travel 
    yield 
    time_travel_return 
    end 

    def time_travel 
    if Time::Clock.fake_time 
     Timecop.travel Time::Clock.fake_time 
    else 
     Timecop.return 
    end 
    end 

    def time_travel_return 
    Timecop.return 
    end 
end 

Time::Clock是我自己的班級,跟蹤假時間。

我有一個單獨的TimeController,可以讓我改變服務器上的時間。

class TimeController < ApplicationController 
    before_action :require_admin! 

    def index 
    end 

    def update 
    @clock.update_attributes params[:time_clock] 
    redirect_to time_index_path 
    end 

    def destroy 
    @clock.reset 
    redirect_to time_index_path 
    end 
end 
+0

謝謝@Kevin爲我工作。它改變了Ruby服務器而不是heroku服務器的時間。 每當我重新啓動Heroku時,它會將日期更改爲當前日期。 (y) –

+0

沒錯。這對測試一段時間內傳播的一系列步驟非常有用。 –

0

對於在Heroku上運行的Rails應用程序,默認情況下,Time.now和some_time.localtime將以UTC顯示。如果您想爲您的應用指定時區,可以將TZ配置變量設置爲時區(必須使用tz數據庫時區格式)。

heroku config:add TZ="America/Los_Angeles"