Rails - Setting time_zone dynamically as per user

2019-02-24 21:53发布

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!

1条回答
Viruses.
2楼-- · 2019-02-24 22:26

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
查看更多
登录 后发表回答