2017-02-24 45 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"