简体   繁体   中英

Rails. Set default timezone when current_user loaded

I'd like to change the application time zone when current_user (Devise) is initialized, like this:

    Time.zone = current_user.settings(:main).time_zone

What is the best place in the app to put this code at (application controller, before_filter is not a solution)?

I think the safest approach here would be to use the around_action like such, make sure to specify which action you want this to happen on:

class SomeController < ApplicationController
    around_action :set_time_zone, only: :show

    private

    def set_time_zone
       if current_user
         Time.zone = current_user.settings(:main).time_zone 
       end
       yield         
    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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM