Ruby on Rails Session Question
Rails 3.0
After doing
rake db:sessions:create
then adding the line of code in the session_store.rb,
CouponManager::Application.config.session_store :active_record_store
What else needs to be done before I'm capable of using sessions like
session[:t开发者_如何学JAVAtl_qty] = 5
That is all you should need to do. You should be able to use that session hash now and it should be persistent. Check out the following code.
Controller
before_filter :set_user
def set_user
if session[:user_id]
@user = User.find(session[:user_id])
end
end
def login
if user = User.find(params)
session[:user_id] = user.id
end
end
View
<div>Hello, <%= @user.email %>!</div>
You can always take a look in the sessions table to make sure that your data is getting there... it should be in the data column (base64 encoded). If you're using sqlite3, you can do something like this:
: sqlite3 db/development.sqlite3
sqlite> select data from sessions limit 1;
... base64 data ...
You can decode the base64 data to see what is making it there. Hope that helps.
精彩评论