的开始日期和到期日期

的开始日期和到期日期

本文介绍了设置 Rails cookie 的开始日期和到期日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将 Rails cookie 设置为在特定日期开始和/或到期?

How do I set a Rails cookie to start and/or expire at a certain date?

推荐答案

摘自 Rails 5 文档:

Cookie 是通过 ActionController#cookies 读取和写入的.

正在读取的 cookie 是与请求一起接收的 cookie,正在写入的 cookie 将与响应一起发送出去.读取 cookie 不会取回 cookie 对象本身,只会取回它持有的值.

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.

写作示例:

# 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 的选项符号是:

The option symbols for setting cookies are:

  • :expires - 此 cookie 过期的时间,作为 Time 或 ActiveSupport::Duration 对象.
  • :expires - The time at which this cookie expires, as a Time or ActiveSupport::Duration object.

[...]

这篇关于设置 Rails cookie 的开始日期和到期日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 14:36