Rails - dynamically setting time_zone of user choice
I thank your help for one of the features that work on my new site!
This refers to dynamic time_zones according to the requirement that the user could choose from a set of predefined time_zones that say us_zones. When a user selects a zone, the entire site must be installed / updated to TimeZone.
However, at this time, the new time zone is not updated in Apache, and the time zone is only updated when the server is restarted.
I was thinking about how to use the Rails Initializer and initialize_time_zone () methods, but even this requires restarting the rails server.
Thanks in advance!
+3
source to share
1 answer
Put something like this in your application controller:
def set_api_time_zone
utc_offset = current_user_session && current_user_session.user ? current_user_session.user.time_zone_offset.to_i.minutes : 0
user_timezone = ActiveSupport::TimeZone[utc_offset]
Time.zone = user_timezone if user_timezone
end
+3
source to share