轨道.在加载current_user时设置默认时区



我想在初始化current_user(Devise)时更改应用程序时区,如下所示:

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

应用程序中放置此代码的最佳位置是什么(应用程序控制器,before_filter不是解决方案)?

我认为这里最安全的方法是像这样使用around_action,确保指定您希望在以下哪个操作上发生这种情况:

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

最新更新