How do I add timezone configuration to my environment.rb file?
First, what is the difference between configuring in my application.rb file vs. my environment.rb fil开发者_开发百科e?
I read that I need to configure my timezone default in my environment.rb file. I am not quite sure how to do that though. So far I have this in my environment.rb file:
# Load the rails application
require File.expand_path('../application', __FILE__)
# Initialize the rails application
SampleApp::Application.initialize!
Do I need to rerun the rails server after make changes? Any additional steps?
Thanks!
In rails 2, it was environment.rb that had all the configuration. In rails 3 it's changed to application.rb, as well as environment/production.rb and environment/development.rb, and Gemfile. To configure timezone, put this in application.rb in the Application class:
class Application < Rails::Application
config.time_zone = 'Eastern Time (US & Canada)'
end
You can run rake time:zones:all
to see a list of available timezones.
edit In rails 3 you don't need to touch environment.rb.
From The Rails (3) Initialization Process:
1.14 config/environment.rb
This file is the common file required by config.ru (rails server) and Passenger. This is where these two ways to run the server meet; everything before this point has been Rack and Rails setup.
This file begins with requiring config/application.rb.
1.15 config/application.rb
This file requires config/boot.rb, but only if it hasn’t been required before, which would be the case in rails server but wouldn’t be the case with Passenger.
Then the fun begins!
To configure your application's timezone, add this to your application.rb:
class Application < Rails::Application
# ...
config.time_zone = 'Pacific Time (US & Canada)' # for example
# ...
end
And, as @bricker points out, rake time:zones:all
displays all available timezones.
精彩评论