简体   繁体   English

将Rails cookie设置为在1天后过期

[英]Setting Rails cookie to expire in 1 day

Looking through the docs here http://api.rubyonrails.org/classes/ActionDispatch/Cookies.html , and setting a Rails cookie as follows: 浏览http://api.rubyonrails.org/classes/ActionDispatch/Cookies.html中的文档,并按如下方式设置Rails cookie:

cookies[:votes_left] = { :value => 3, :expires => 24.hours.from_now }

Inside my browser (Chrome), it's reporting the correct value but is giving me Invalid Date under the Expires column (Network > Cookies). 在我的浏览器(Chrome)中,它报告了正确的值,但在Expires列(网络> Cookie)下给我Invalid Date

Am I setting the date incorrectly? 我是否错误地设定了日期? I've tried variations on the Time object, but keep getting the same result, a correctly valued cookie with an invalid expiration . 我已尝试过Time对象的变体,但仍然得到相同的结果,一个带有无效过期正确值cookie

Tested this out myself and I see no issue with your code. 我自己测试了这一点,我发现你的代码没有问题。 As @Nevir said, check whether your server clock is correct. 正如@Nevir所说,检查你的服务器时钟是否正确。 The only issue with that, if it's the wrong server time and it's actually setting the expiration before today's date, it won't set the cookie. 唯一的问题是,如果它是错误的服务器时间并且它实际上在今天的日期之前设置了到期,它将不会设置cookie。

Try adding Rails.logger 24.hours.from_now just to see what it outputs. 尝试添加Rails.logger 24.hours.from_now只是为了看看它输出了什么。

But yes, you are setting the expires parameter properly. 但是,是的,您正在正确设置expires参数。

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

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