I came across this issue today in a Rails app I am writing. It took me a while to figure out so hopefully this post will save someone else a bit of time. My dev environment is Rails 3.1.3 using Ruby 1.9.3 – although this may affect other versions.
My Rails app has a custom default timezone, with the following set in config/environment.rb:
config.time_zone = 'Brisbane'
There are no issues with Date attributes. The time zone is respected through the Rails app when creating or displaying DateTime attributes. However, when editing DateTime attributes, the time displayed in the form is in the UTC time zone rather than local time.
After trying numerous workarounds, I found a post on Stack Overflow mentioning that this appears to be an issue with displaying DateTimes in text fields in Rails. It appears that this is a limitation (bug?) in Rails. As I’m using a JQuery-UI DateTimePicker plugin, I have to use a text field to display the time.
As mentioned in the Stack Overflow post, to fix this issue I needed to force the attribute to be used by the text field by changing the text field in my form from:
f.text_field :start_time
to:
f.text_field :start_time, value: f.object.start_time
Fixed.