Rails Cookie,设置开始日期和到期日期 [英] Rails cookies, set start date and expire date

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

问题描述

Rails Cookie,

Rails cookies,

我需要使用Cookie设置开始日期和到期日期,

i need to set start date and expire date using cookie,

推荐答案

Cookie是通过 ActionDispatch#cookies (Rails 3及更低版本中的ActionController#cookies)。此答案中的文字来自上面的API文档链接。

Cookies are read and written through ActionDispatch#cookies (ActionController#cookies in Rails 3 and below). The text in this answer is quoted from the API docs link above.

正在读取的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.
  cookies[:user_name] = "david"

  # Sets a cookie that expires in 1 hour.
  cookies[:login] = { :value => "XJ-122", :expires => 1.hour.from_now }

阅读示例:

  cookies[:user_name] # => "david"
  cookies.size        # => 2

删除示例:

  cookies.delete :user_name

请注意,域设置Cookie时,您还必须在删除Cookie时指定域:

Please note that if you specify a :domain when setting a cookie, you must also specify the domain when deleting the cookie:

 cookies[:key] = {
   :value => 'a yummy cookie',
   :expires => 1.year.from_now,
   :domain => 'domain.com'
 }

 cookies.delete(:key, :domain => 'domain.com')

设置Cookie的选项符号为:

The option symbols for setting cookies are:

* :value - The cookie’s value or list of values (as an array).
* :path - The path for which this cookie applies. Defaults to the root of the application.
* :domain - The domain for which this cookie applies.
* :expires - The time at which this cookie expires, as a Time object.
* :secure - Whether this cookie is a only transmitted to HTTPS servers. Default is false.
* :httponly - Whether this cookie is accessible via scripting or only HTTP. Defaults to false.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆