2015-02-11 59 views
0

我想更改应用程序时区时,CURRENT_USER(设计)被初始化,这样的:Rails。 CURRENT_USER加载时设置默认时区

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

什么是应用最好的地方,把这个代码在(应用程序控制器,before_filter不是解决方案)?

+0

为什么''ApplicationController''是不是一个解决方案?这将是我的建议... – dgilperez 2015-02-11 13:53:01

+0

Aggree与@dgilperez。 'ApplicationController'''before_filter'是解决方案。 – pierallard 2015-02-11 14:27:11

+0

我不相信更改应用程序时区是正确的方法。如果用户更改时区怎么办? (或者它改变了夏令时? – 2015-02-11 15:28:39

回答

1

我觉得这里是最安全的方法是使用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 
+0

ApplicationController的问题是其他一些控制器没有权限访问current_user(比如API控制器),但是“如果current_user”应该解决这个问题。 :)非常感谢你的建议! – Serge 2015-02-12 01:39:27

+0

没问题Serge !,很高兴帮助,请标记答案,如果接受:) – DevDude 2015-02-12 16:54:13