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.
EDIT: One might ask why not just use the one that works. The problem with this is that I am using a nested attribute so i can't exactly do this. I tried doing this:
# from my real app:
<% appt.fields_for :time_slot do |ts| %>
<%=h ts.object.start_at.strftime('%T') %>
<%= hidden_field ts.object.start_at, :start_at %>
<%= hidden_field ts.object.end_at, :end_at %>
<% end %>
but the html results as:
<div>
10:00:00
<input id="2010-05-30_10:00:00_-0500_start_at" name="2010-05-30 10:00:00 -0500[start_at]" type="hidden">
<input id="2010-05-30_10:10:00_-0500_end_at" name="2010-05-30 10:10:00 -0500[end_at]" type="hidden">
<input id="appointment_block_appointments_attributes_0_time_slot_attributes_id" name="appointment_block[appointments_attributes][0][time_slot_attributes][id]" type="hidden" value="95">
</div>