Joh*_*per 9 ruby-on-rails actionview actionviewhelper ruby-on-rails-3
我正在使用一个音乐会巡回网站的应用程序,所有时间(公告时间,开始时间和活动开始时间)都在每个特定地点的时区本地.我将用户输入的日期/时间视为适用,并运行before_filter来设置适当的时区,以便所有内容都以UTC格式存储在数据库中.对于"新"表单以及在索引和显示操作中显示时间,完全没问题.当数据从数据库带回到视图中时,我使用in_time_zone来根据特定的场地进行调整.
唯一的问题是编辑表格.日期/时间选择以UTC显示数据.当我在网站上工作时,我会进行心理调整,但对于其他人而言,这是令人困惑的.我想做一些事情:
<%= f.datetime_select :start_datetime.in_time_zone(@event.time_zone) %>
或者,在控制器中:
def edit
@performance = Performance.find(params[:id])
@event = @performance.event
@performance.start_datetime = @performance.start_datetime.in_time_zone(@event.time_zone)
end
Run Code Online (Sandbox Code Playgroud)
然后简单地说,<%= f.datetime_select :start_datetime %>.
不幸的是,我没有找到正确的方法来做到这一点.你有什么值得尝试的想法吗?
非常感谢你.
像这样的事情怎么样:
# change PST to correct zone abbrev.
new_zone = ActiveSupport::TimeZone.new("PST")
@performance.start_datetime = @performance.start_datetime.in_time_zone(new_zone)
Run Code Online (Sandbox Code Playgroud)