5

I specified config.time_zone in my Rails app, but the retrieved times in form fields still render as the UTC (which creates problems on updates). Shouldn't this be converted to local time in the specified zone?

/config/application.rb (relevant lines only):

module ExampleSite
    class Application < Rails::Application

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

/events/edit.html.erb (full file):

<h1>Edit This Event</h1>
<%= form_for(@event) do |f| %>
    <%= render 'fields', :f => f %>
    <div class="actions">
        <%= f.submit "Update Event" %>
    </div>
<% end %>

/events/_fields.html.erb (relevant lines only:)

<div class="field">
    <%= f.label      :time_start, "Start Time" %><br />
    <%= f.text_field  :time_start, :class => "datetimefield" %>
</div>
<div class="field">
    <%= f.label      :time_end, "End Time (if applicable)" %><br />
    <%= f.text_field  :time_end, :class => "datetimefield" %>
</div>

When I enter the datetime string to create a new event, the value is saved properly (in UTC) and rendered in my views as desired (in the local time zone) where it had been rendering UTC before the config.time_zone switch (so I know the switch was made).

But when I go to edit any other attribute of the event, the time rendered to the form field in the /edit view is the UTC time--which means when I update the event, the time is re-saved as though the time had been re-entered and presumed local, which shifts the time by 5 hours (my local difference from UTC) as the system converts the "updated" time attribute to UTC for storage.

How can I make the localized time be rendered in my form fields?

Running Rails 3.0.5, deploying to Heroku (though the problem exists in both development and production environments)

jasonmklug
  • 1,584
  • 2
  • 15
  • 28
  • 1
    Can you post the code from your view that generates the form? – Teddy Jun 13 '11 at 20:57
  • i usually add a formated_date method in my models that use time. something like this: def format_date(date) date.getlocal.strftime("%m/%d/%y") end, getlocal converts the utc time to the server's local – corroded Jun 14 '11 at 18:15
  • 1
    After looking deeper into it, I've determined that everything is working correctly *except* for the form fields. The UTC datetime from the database should be converted to the proper zone as it's populated in the time_start and time_end fields, but instead it's just populating those fields with the UTC. Is this not covered by the config.time_zone setting? And if not, how can I populate the fields with properly-zoned times? – jasonmklug Jun 14 '11 at 18:23
  • Why you don't use DateHelper ? http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html – Dinatih Jun 29 '11 at 20:12
  • 1
    @Dinatih I'm looking at it, but I'm not sure how I would use DateHelper to load the correct times into my form fields in the 'edit' view. Please put that into an answer, if you can. – jasonmklug Jul 01 '11 at 20:51

3 Answers3

9

It turns out the real problem was in the text_fields themselves.

My 'config.time_zone' setting was working just fine (without any extra methods or hacks) in my 'index' and 'show' views, and it worked in the 'edit' view, too, as long as I used a datetime_select instead of a text_field.

As this wasn't an option for me (I was using jQuery UI's DatePicker, which needs a text_field), I investigated the text_field specific problem and answered another StackOverflow question of my own on the subject.

If you're having the text_field problem, check out that question/answer.

Community
  • 1
  • 1
jasonmklug
  • 1,584
  • 2
  • 15
  • 28
1

Just use this:

  <div class="field">
    <%= f.label :time_start %><br />
    <%= f.datetime_select :time_start, :class => "datetimefield" %>
  </div>

I create a little app for this and it's works.

Dinatih
  • 2,456
  • 1
  • 21
  • 21
0

You can do something like this:

heroku config:add TZ=America/Chicago

That should fix your issue on Heroku.

nbucciarelli
  • 460
  • 2
  • 6
  • 16
  • I tried this, but it didn't seem to affect anything (same incorrect times with same offset loaded into my forms). Is there another config option for my app I should look at as well? I tried this both with the config.time_zone and without it. – jasonmklug Jul 01 '11 at 20:53