2013-12-09 72 views
0

如何將時區更改爲歐洲/阿姆斯特丹。 我試圖把在application_controller.rbRoR 4更改時區

config.time_zone = "Europe/Amsterdam" 

回答

2

您可以設置自定義時區的情況下在config/application.rb

config.time_zone = 'Central Time (US & Canada)' 

OR

的ApplicationController

使用@country來管理你想要切換到哪個時區。 在這個例子中,我使用「子域」來獲取@country。您可以輕鬆地適應您的方案

寫在你的/app/controller/application_controller.rb

class ApplicationController < ActionController::Base 

     before_filter :set_country 
     before_filter :set_time_zone 

    private 

     def set_country 
     if request.subdomains.empty? || request.subdomains.first == 'www' 
      redirect_to(:subdomain => "uk") 
     else 
      @country ||= request.subdomains.first.upcase 
     end 
     @country ||= "IT" 
     end 

     def set_time_zone 
     case @country 
     when 'IT' 
      Time.zone = 'Rome' 
     when 'US' 
      Time.zone = 'Central Time (US & Canada)' 
     when 'UK' 
      Time.zone = 'London' 
     end 
     end 
    end 
0

您可以設置自定義時區在config/application.rb

# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone. 
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC. 
# config.time_zone = 'Central Time (US & Canada)'