how to store hash value in cookie with rails 3
I want to implement a remember me option for the login
theref开发者_开发知识库ore 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?
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'
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
Maybe this answer can help you
精彩评论