Rails Cookie,设置开始日期和到期日期

编程入门 行业动态 更新时间:2024-10-11 21:20:24
本文介绍了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' } cookies.delete(:key, :domain => 'domain')

设置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,设置开始日期和到期日期

本文发布于:2023-10-31 09:07:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1545676.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:日期   Rails   Cookie

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!