Why does Rails screw up timezones when I am editing a resource?
Posted
by DJTripleThreat
on Stack Overflow
See other posts from Stack Overflow
or by DJTripleThreat
Published on 2010-05-29T19:28:17Z
Indexed on
2010/05/29
19:32 UTC
Read the original article
Hit count: 244
Steps to produce this:
prompt>rails test_app
prompt>cd test_app
prompt>script/generate scaffold date_test my_date:datetime
prompt>rake db:migrate
now edit your app/views/date_tests/edit.html.erb
:
<h1>Editing date_test</h1>
<% form_for(@date_test) do |f| %>
<%= f.error_messages %>
<p>
RIGHT!<br/>
<%= text_field_tag @date_test, f.object.my_date %>
</p>
<p>
WRONG!<br />
<%= f.text_field :my_date %>
</p>
<p>
<%= f.submit 'Update' %>
</p>
<% end %>
<%= link_to 'Show', @date_test %> |
<%= link_to 'Back', date_tests_path %>
now edit your config/environment.rb
:
#add this
config.time_zone = 'Central Time (US & Canada)'
This recreates the problem I am having in my actual app. The problem with my app is that I'm storing a date in a hidden field and rendering a "user friendly" version. Creating a resource works fine but as soon as I try to edit it the time changes (it adds the difference between my current time zone configuration and UTC). go to http://localhost:3000/date_tests/new and save the time then go to reedit it and you will have two different representations of the date/time one which will save incorrectly and the other that will.
© Stack Overflow or respective owner