开发者

ActiveRecord model with datetime stamp with timezone support attribute

开发者 https://www.devze.com 2022-12-17 08:41 出处:网络
Rails is great that it will support timezone overall in the application with Time.zone. I need to be able to support the timezone a user selects for a record. The user will be able to select date, tim

Rails is great that it will support timezone overall in the application with Time.zone. I need to be able to support the timezone a user selects for a record. The user will be able to select date, time, and timezone for the record and I would like all calculations to be done with respect to the user selected timezone.

My question is what is the best practice to handle user selected timezones. The model is using a time_zone_select and datetime_select for two different attributes timezone and scheduled_at. When the model saves, the scheduled_at attribute gets converted to the locally defined Time.zone.

When a user goes back to edit the scheduled_at attribute with the datetime_select the datetime is set to the converted Time.zone timezone and not the timezone attrib开发者_如何学编程ute. Is there a nice way to handle to the conversion to the user selected timezone?


You could handle this in a before_filter, either in the specified controller or application wide. See this example:

# controllers/application.rb
before_filter :set_user_time_zone

private

def set_user_time_zone
  Time.zone = current_user.timezone if logged_in?
end

Ryan Bates made a screencast about this: Time Zones in Rails 2.1

0

精彩评论

暂无评论...
验证码 换一张
取 消