Rails: A summary of cookies

Source: Internet
Author: User

The cookie is sent to the server for reading with each request from the browser, and the server carries a cookie in the answer (response) written on the computer. Therefore, the cookie is stored locally. And because of this nature of the cookie, the cookie must be "effective" in the next request after assignment (though it doesn't seem to be effective immediately ...). )。
The cookies in rails are fairly simple to use, just use the hash of the cookie as needed. For example:
# sets a simple session cookie.
# This cookie would be deleted when the user's browser is closed.
Cookies[:user_name] = "David"
# Assign A array of values to a cookie.
Cookies[:lat_lon] = [47.68,-122.37]

The corresponding key-value pair is also called by the cookie when reading. By default, the cookie expires after 30min (or expires when the browser is closed).
Of course, you can also set its own expiration date (expires)
Cookies[:login] = {: value = "XJ-122",: expires = 1.hour.from_now}
In fact, the cookie is set up: Value:expires, there are some other parameters:
:p Ath to set a valid path for the cookie. There is no validation of how to use it, but it should refer to which level of directory the cookie is valid in, which prevents different controllers from having the same name cookie.
:d Omain Set the name of the cookie to which it is valid.
: Secure True/false, which identifies whether to be passed to HTTPS server.
: HttpOnly as the name implies.

In addition, you can also set some special cookies
COOKIES.SIGNED[:USER_ID] = current_user.id
Signed cookies to prevent users from tampering with their values (haven't tried, don't know the right to understand)
Cookies.permanent[:login] = "XJ-122"
Permanent cookies, cookies that are valid for 20 years
Both of these cookies can also be used in tandem to form signed permanent cookies.
==============================================================

Rails: A summary of cookies

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.