【问题标题】:jstz time zone in Rails does not seem to update if user changes system timezone如果用户更改系统时区,Rails 中的 jstz 时区似乎不会更新
【发布时间】:2015-04-10 15:52:06
【问题描述】:

我有一个http://thisbythem.com/blog/clientside-timezone-detection/ 的实现。以下是关键文件/代码:

app/controllers/application_controller.rb

around_filter :set_timezone
...
private

  def set_timezone
    if(current_learner)
      timezone = Time.zone = current_learner.time_zone
    else
      timezone = Time.find_zone(cookies[:timezone])
    end
    Time.use_zone(timezone) { yield }
  end

app/views/layouts/application.html.erb

<script class="code" type="text/javascript">
$(function() {
   $('#flash_notification').hide().delay(500).slideDown('slow', function() {
      $(this).delay(4000).fadeOut();
   });
   $("#flash_notification").live('click', function(){
       $(this).stop().fadeOut();;
   });
});
//set event times for non-auth users
var tz = jstz.determine();
$.cookie('timezone', tz.name(), { path: '/' });
</script>

除了上面添加的 jquery.cookie.js 和 jstz.main.js 到 app/assets/js。首次访问该站点时时区 cookie 设置正确,但是如果我更改系统时区并刷新,我看不到 cookie 中的更改。此外,如果我进入 js 控制台并执行以下操作:

tz=jstz.determine();
tz.name();

...我在这里也没有看到更新的时区设置。同时,如果我删除 cookie 并刷新,我会得到新的时区。

【问题讨论】:

  • 能不能把js代码设置cookievar tz = jstz.determine(); $.cookie('timezone', tz.name(), { path: '/' });移到$(function() {里面

标签: javascript ruby-on-rails


【解决方案1】:

原来我使用的是 jstz 文件。我已经拉下了 jstz.main.js 而不是 jstz.js 供参考,这是您需要的:

https://bitbucket.org/pellepim/jstimezonedetect/src/823d744c9757481f0e33a6fea462514aa05fb30a/dist/jstz.js

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-30
    • 2013-01-19
    • 1970-01-01
    • 2017-09-07
    相关资源
    最近更新 更多