Ruby-on-rails 设置 Rails cookie 的开始日期和到期日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1232174/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set start date and expiration date for Rails cookies
提问by Senthil Kumar Bhaskaran
How do I set a Rails cookie to start and/or expire at a certain date?
如何设置 Rails cookie 以在特定日期开始和/或过期?
回答by Senthil Kumar Bhaskaran
Excerpts from Rails 5 documentation:
摘自Rails 5 文档:
Cookies are read and written through ActionController#cookies.
The cookies being read are the ones received along with the request, the cookies being written will be sent out with the response. Reading a cookie does not get the cookie object itself back, just the value it holds.
Examples of writing:
# Sets a simple session cookie. # This cookie will be deleted when the user's browser is closed. cookies[:user_name] = "david" # Sets a cookie that expires in 1 hour. cookies[:login] = { value: "XJ-122", expires: 1.hour } # Sets a cookie that expires at a specific time. cookies[:login] = { value: "XJ-122", expires: Time.utc(2020, 10, 15, 5) } # Sets a "permanent" cookie (which expires in 20 years from now). cookies.permanent[:login] = "XJ-122"[...]
The option symbols for setting cookies are:
:expires- The time at which this cookie expires, as a Time or ActiveSupport::Duration object.[...]
Cookie 是通过 ActionController#cookies 读取和写入的。
正在读取的 cookie 是与请求一起接收的 cookie,正在写入的 cookie 将与响应一起发送出去。读取 cookie 不会取回 cookie 对象本身,只会取回它所持有的值。
写作的例子:
# Sets a simple session cookie. # This cookie will be deleted when the user's browser is closed. cookies[:user_name] = "david" # Sets a cookie that expires in 1 hour. cookies[:login] = { value: "XJ-122", expires: 1.hour } # Sets a cookie that expires at a specific time. cookies[:login] = { value: "XJ-122", expires: Time.utc(2020, 10, 15, 5) } # Sets a "permanent" cookie (which expires in 20 years from now). cookies.permanent[:login] = "XJ-122"[...]
设置 cookie 的选项符号是:
:expires- 此 cookie 过期的时间,作为 Time 或 ActiveSupport::Duration 对象。[...]
回答by pageman
your question might be related to this question: How to I dynamically set the expiry time for a cookie-based session in Rails
您的问题可能与这个问题有关: 如何在 Rails 中为基于 cookie 的会话动态设置到期时间
one of the comments points to Deprecating SlideSessions:
评论之一指向Deprecating SlideSessions:
"..If you need to set expiration period for sessions through all controllers in your application, simply add the following option to your config/intializers/session_store.rb file:
:expire_after => 60.minutesIf you need to set different expiration time in different controllers or actions, use the following code in action or some before_filter:
request.session_options = request.session_options.dup request.session_options[:expire_after]= 5.minutes request.session_options.freezeDuplication of the hash is needed only because it is already frozen at that point, even though modification of at least :expire_after is possible and works flawlessly..."
"..如果您需要通过应用程序中的所有控制器为会话设置过期期限,只需将以下选项添加到您的 config/intializers/session_store.rb 文件中:
:expire_after => 60.minutes如果需要在不同的控制器或动作中设置不同的过期时间,请在动作中使用以下代码或一些before_filter:
request.session_options = request.session_options.dup request.session_options[:expire_after]= 5.minutes request.session_options.freeze只需要复制散列,因为它在那时已经被冻结,即使至少修改 :expire_after 是可能的并且可以完美地工作......”
I hope that helps. :)
我希望这有帮助。:)
回答by eltiare
It's worth noting that as of right now it is impossible to set a start time for a cookie. A cookie set is always active immediately.
值得注意的是,目前还无法为 cookie 设置开始时间。cookie 集始终立即处于活动状态。

