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