Rails 3: setting the timezone to the current users timezone

前端 未结 3 1589
花落未央
花落未央 2020-12-03 00:00

I put this in Application Controller:

before_filter :set_timezone 

def set_timezone  
Time.zone = current_user.time_zone 
end  

But I alwa

3条回答
  •  鱼传尺愫
    2020-12-03 00:34

    Further to Jesse's answer, I should add that you can generally avoid adding a new column in db and just create a custom method in user model and make use of cookie to get the user's timezone:

    in client-side (js):

    function set_time_zone_offset() {
        var current_time = new Date();
        $.cookie('time_zone', current_time.getTimezoneOffset());
    }
    


    in Application Controller:

    before_filter :set_timezone 
    
    def set_timezone  
     min = request.cookies["time_zone"].to_i
     Time.zone = ActiveSupport::TimeZone[-min.minutes]
    end 
    

提交回复
热议问题