In my application_controller.rb
before_action :set_user_time_zone
def set_user_time_zone
Time.zone = 'Kolkata'
end
I added the above code to change the Time-zone dynamically. But it does not change the Time-zone.I have time_zone field for each user in user table, I want to change time_zone according to current user. How to change Time.Zone dynamically?
You could put a filter in your application controller that will apply to all actions in all controllers to use the user's time zone when dealing with dates and times.
This code to do that comes from Sebastian Porto and assumes you have a :time_zone
column for users. It defaults to using UTC if it can't get the user time zone.
class ApplicationController < ActionController::Base
around_filter :set_time_zone
def set_time_zone(&block)
time_zone = current_user.try(:time_zone) || 'UTC'
Time.use_zone(time_zone, &block)
end
end
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.