Rails - Setting time_zone dynamically as per user selection

ε祈祈猫儿з 提交于 2019-12-12 13:47:30

问题


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:


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


来源:https://stackoverflow.com/questions/9800301/rails-setting-time-zone-dynamically-as-per-user-selection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!