简体   繁体   English

使用JavaScript从客户端在Rails上设置时区

[英]Setting timezone on Rails from clientside using javascript

I have a webapp that sets timezone 我有一个设置时区的webapp

post_controller.rb

before_filter :set_time_zone
def set_time_zone
  Time.zone = user.time_zone
end

Now, instead of getting the time_zone from the user at registration, I was wondering how I'd set the timezone dynamically from the client side and set it in the before_filter. 现在,我没有想从注册时从用户那里获取time_zone,而是想知道如何从客户端动态设置时区并将其设置在before_filter中。 I was trying to use detect_timezone_rails gem . 我正在尝试使用detect_timezone_rails gem The gem provides an easy way to access clientside timezone, simply by calling the function like this from js file. gem提供了一种简单的方法来访问客户端时区,只需从js文件中调用此类函数即可。

$(document).ready(function(){
    $('#your_input_id').set_timezone(); 
});

Now, the above code automatically sets your hiddenfield input or select input, but I was wondering if you could simply use the function call to save to session and retrieve it from Rails server. 现在,以上代码自动设置了hiddenfield输入或select输入,但是我想知道您是否可以简单地使用函数调用保存到会话并从Rails服务器检索它。 I guess when the user first visits the site, the timezone can be set and the session value can be used to set timezone for the rest of the visit. 我猜想当用户第一次访问该站点时,可以设置时区,而会话值可以用于为其余访问设置时区。 I'd think it'd be possible to use the session value to set timezone in the before filter. 我认为可以使用session值在before过滤器中设置时区。 Being pretty new to javascript, I'm not sure how to access Rail's encrypted cookie store to set the value. 对于javascript来说还很陌生,我不确定如何访问Rail的加密cookie存储来设置值。 Is this possible? 这可能吗? if so, how can I do this? 如果是这样,我该怎么办? thanks in advance, 提前致谢,

#javascript
function readCookieValue(cookieName)
{
  return (result = new RegExp('(?:^|; )' + encodeURIComponent(cookieName) + '=([^;]*)').exec(document.cookie)) ? decodeURIComponent(result[1]) : null;
}

$(document).ready(function(){

if(readCookieValue("time_zone") === null) {
  $.post('/set_time_zone',
       { 'offset_minutes':(-1 * (new Date()).getTimezoneOffset())});
}

#controller:
def set_time_zone
  offset_seconds = params[:offset_minutes].to_i * 60
  @time_zone     = ActiveSupport::TimeZone[offset_seconds]
  @time_zone     = ActiveSupport::TimeZone["UTC"] unless @time_zone
  if @time_zone
    cookies[:time_zone] = @time_zone.name if @time_zone
    render :text => "success"
  else
    render :text => "error"
  end
end

We did this somewhat differently. 我们这样做有些不同。 We use the gon gem to set a variable on the Rails side if we want to collect the timezone from JS. 如果要从JS收集时区,可以使用gon gem在Rails侧设置一个变量。 Then we have JS code on the client that examines that variable, and if set, does an XHR post to an endpoint (like the OP did) with the timezone string as returned by the jstimezonedetect script, which returns an IANA timezone key. 然后,我们在客户端上具有检查该变量的JS代码,并且如果设置了该代码 ,则使用jstimezonedetect脚本返回的时区字符串将XHR发布到端点(如OP一样),该脚本返回一个IANA时区密钥。 Finally, to convert this to a Rails 3.2.19 timezone name, we did ActiveSupport::TimeZone::MAPPING.invert[iana_key] . 最后,要将其转换为Rails 3.2.19时区名称,我们做了ActiveSupport::TimeZone::MAPPING.invert[iana_key] It took a few steps to figure this out, hope it helps someone. 它采取了一些步骤来解决此问题,希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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