我在 Rails 应用程序中指定了 config.time_zone,但表单字段中检索到的时间仍然呈现为 UTC(这会在更新时产生问题)。难道不应该将其转换为指定区域的当地时间吗?
/config/application.rb(仅限相关行):
module ExampleSite
class Application < Rails::Application
config.time_zone = 'Central Time (US & Canada)'
end
end
/events/edit.html.erb(完整文件):
<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(仅相关行:)
<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>
当我输入日期时间字符串来创建新事件时,该值会正确保存(以 UTC 格式)并根据需要在我的视图中呈现(以本地时区表示)had正在渲染 UTCbeforeconfig.time_zone 开关(所以我知道已经进行了开关)。
但是,当我去编辑事件的任何其他属性时,呈现到 /edit 视图中的表单字段的时间是 UTC 时间 - 这意味着当我更新事件时,时间会重新保存,就好像时间已经已重新输入并假定为本地时间,这会将时间移动 5 小时(我的本地时间与 UTC 的差异),因为系统将“更新的”时间属性转换为 UTC 进行存储。
如何在我的表单字段中呈现本地化时间?
运行Rails 3.0.5,部署到Heroku(尽管开发和生产环境都存在问题)