Created
December 14, 2013 16:50
-
-
Save TechFounder/7961660 to your computer and use it in GitHub Desktop.
Cookies syntax built into Rails
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module SessionsHelper | |
def sign_in(user) | |
cookies.permanent[:remember_token] = user.remember_token | |
self.current_user = user | |
end | |
end | |
# You can use cookies as if it were a hash even though it's not as it's actually saves a piece of text on the bowser. | |
# Each element in the cookies is itself a hash of two elements, a value and an optional expires date. | |
cookies[:remember_token] = { value: user.remember_token, | |
expires: 20.years.from_now.utc } | |
# This is same as using the permanent method where rails will automatically set 20.years.from_now: | |
cookies.permanent[:remember_token] = user.remember_token | |
# After the cookie is set, on subsequent page views we can retrieve the user with code like: | |
User.find_by_remember_token(cookies[:remember_token]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment