Sumanth Krishna
Sumanth Krishna

Reputation: 55

Rails - Setting time_zone dynamically as per user selection

I appreciate your help one of the features working on for my new website!

This is regarding the dynamic time_zones as per the requirement user would be able to choose from set of pre-defined time_zones say us_zones. When the user picks the zone the entire site should be set/updated to TimeZone.

However, at present the new time zone is not becoming updated into Apache and the updation of time zone happens only at the restart of the server.

I was thinking in the lines of using Rails Initializer class and initialize_time_zone() methods, but even this requires the rails server to be restarted.

Thanks in advance!

Upvotes: 3

Views: 3141

Answers (1)

gayavat
gayavat

Reputation: 19398

Place to Application controller something like this:

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

Upvotes: 3

Related Questions