Skip to content

Instantly share code, notes, and snippets.

@TechFounder
Created December 14, 2013 16:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TechFounder/7961660 to your computer and use it in GitHub Desktop.
Save TechFounder/7961660 to your computer and use it in GitHub Desktop.
Cookies syntax built into Rails
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