9

I'm working with an app for a concert tour website, where all times (announcement times, on-sale start times, and event start times) are local to each particular venue's time zone. I take the user entered date/time where applicable and run a before_filter to set the appropriate time zone so that everything is stored in the database in UTC. For the 'new' form and for displaying the times in index and show actions, no problem at all. When the data is brought back out of the database and into a view, I use in_time_zone to adjust per the particular venue.

The only issue is on the edit form. The date/time select is showing the data in UTC. When I'm working on the site, I mentally adjust, but for others it's confusing. I'd like to do something along the lines of:

<%= f.datetime_select :start_datetime.in_time_zone(@event.time_zone) %>

Or, in the controller:

def edit
  @performance = Performance.find(params[:id])
  @event = @performance.event
  @performance.start_datetime = @performance.start_datetime.in_time_zone(@event.time_zone)
end

Then simply, <%= f.datetime_select :start_datetime %>.

Unfortunately, I haven't found the right way to do this. Do you have any ideas worth giving a shot?

Thank you very much.

4

4 回答 4

3

您可以使用 datetime_select 的默认方法,如下所示:

%br
= f.label :req_sess_start, "Session starts at"
= f.datetime_select(:req_sess_start, :start_year => 2010, :ampm => true, :default => 0.days.from_now.in_time_zone(@timezone))

由于显示的默认值,客户端将假定必须在他/她的本地时区输入时间,但是......这个值实际上将在您的应用程序的默认时区中(如 application.rb 中提供的,默认是UTC)。因此,您需要一些服务器端编码才能将其转换为正确的值。

于 2012-05-30T17:23:42.137 回答
1

我不确定我是否理解您想要做什么,但由于您要存储@event.time_zone,您可以添加

:default => start_time.in_time_zone(@event.time_zone)

到您的 datetime_select 表单字段。

于 2011-10-24T16:55:18.587 回答
0

像这样的东西怎么样:

# change PST to correct zone abbrev.
new_zone = ActiveSupport::TimeZone.new("PST")
@performance.start_datetime = @performance.start_datetime.in_time_zone(new_zone)
于 2011-09-26T00:25:41.053 回答
0

我刚刚注意到这是一篇旧帖子,但是:如果我是你,我会使用虚拟属性来表示场地的日期时间。例如,您可以为性能添加一个名为adjusted_start_time 的属性。

于 2012-01-29T14:59:41.310 回答