Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 812
Sumanth Krishna Avatar asked Mar 29 '26 22:03

Sumanth Krishna


1 Answers

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
like image 155
gayavat Avatar answered Apr 02 '26 05:04

gayavat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!