填充“/edit”的表单字段时,Rails 应用程序 config.time_zone 不适用看法

发布于 2024-11-15 09:29:32 字数 1414 浏览 1 评论 0原文

我在 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 为单位),并根据需要在我的视图中呈现(以本地时区),在 config.time_zone 切换之前已呈现 UTC (所以我知道已经进行了切换)。

但是,当我去编辑事件的任何其他属性时,呈现到 /edit 视图中表单字段的时间是 UTC 时间 - 这意味着当我更新事件时,时间会重新保存,就好像时间已经已重新输入并假定为本地时间,这会将时间移动 5 小时(我的本地时间与 UTC 的差异),因为系统将“更新的”时间属性转换为 UTC 进行存储。

如何在表单字段中呈现本地化时间?

运行 Rails 3.0.5,部署到 Heroku(尽管开发和生产环境中都存在问题)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

画▽骨i 2024-11-22 09:29:32

事实证明,真正的问题在于文本字段本身。

我的“config.time_zone”设置在“index”和“show”视图中工作得很好(没有任何额外的方法或黑客),并且它在“edit”视图中也工作,只要我使用 datetime_select 代替一个文本字段的。

由于这对我来说不是一个选择(我使用的是 jQuery UI 的 DatePicker,它需要一个 text_field),我调查了 text_field 特定问题并 回答了我自己关于该主题的另一个 StackOverflow 问题

如果您遇到 text_field 问题,请查看该问题/答案。

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.

茶色山野 2024-11-22 09:29:32

只需使用这个:

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

我为此创建了一个小应用程序并且它有效。

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.

梦罢 2024-11-22 09:29:32

你可以这样做:

heroku config:add TZ=America/Chicago

这应该可以解决你在 Heroku 上的问题。

You can do something like this:

heroku config:add TZ=America/Chicago

That should fix your issue on Heroku.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文