Session values aren't kept

128 views Asked by At

I'm using a JRuby(latest, 1.7 RC2)/Sinatra mix on the Trinidad server, for background info.

get "/" do
    @user = session[:user] || [3,2]
    puts session[:user]
    haml :home
end

get "/signed_in" do
    "#{session[:loggedin]}"
end

post "/signup" do
    user = User.create(:username => params[:username], :password => Digest::SHA1.hexdigest(params[:password]))
    session[:user] = user
    session[:loggedin] = true
    puts session[:user]
end'

What I expect as output is (with comments):

# blank line
#<User:0x4049839a>
#<User:0x4049839a>

But what I really get is:

# blank line
#<User:0x4049839a>
# blank line

And after the post, /signed_in will still have no value to output, when it really should be true.

Why aren't these values being kept? No, I'm not using shotgun, I do have sessions enabled, and I do have a session secret set up.

1

There are 1 answers

0
wich On BEST ANSWER

You cannot keep DataMapper resources in session variables. Instead put the key to your user object in your session variable and use a before helper to set @user = User.first(session[:user]) and use the @user instance variable throughout your application

Also, you do not need the session[:loggedin] entry, just use @user.nil?