gilsilas
gilsilas

Reputation: 1471

how to store hash value in cookie with rails 3

I want to implement a remember me option for the login

therefore I want to store both username and hashed password to a cookie, and check it when the user come back to the site.

I tried to do

cookies[:user] = { :value => {:username => @user.username, :password =>
@user.hashed_password}, :expires => 1.month.from_now }

it's save the cookie, but I can't read its attributes

cookies[:user].username # doesn't work

by the way, is it the best solution to implement RememberMe?

Upvotes: 4

Views: 7454

Answers (2)

lulalala
lulalala

Reputation: 17991

From what I have read, one can only store strings in cookies. In order to store other data structures one need to serialize the data, such as JSON or YAML.

And I guess Session object has this built-in

Upvotes: 3

smathy
smathy

Reputation: 27971

Better to use the session method for setting these sorts of things. Feel free to keep the session in a cookie by using the default :cookie_store setting in config/initializers/session_store.rb.

session[:user] = { :foo => { :bar => 'woot' } }

Then later...

session[:user][:foo][:bar] # => 'woot'

Upvotes: 3

Related Questions