alilland alilland - 10 days ago 4
Ruby Question

session[:id] variable not being picked up within another route from sinatra

I am following this tutorial here on setting up a user login https://learn.co/lessons/sinatra-user-auth

It says that in order to save a

session[:id]
in my route I would do something like this:

post '/login' do
@user = User.get_id(params['email'],params['password'])
session[:id] = @user # equals a numeric value of the matching user
redirect '/home'
end


however, when i get to my
/home
route I am no longer able to access the
session[:id]
that was set in my
/login
route, it returns
nil


# this doesnt return the session id
# consequently I cant lookup my user records
get '/home' do
@user = User.object(session[:id]) # looks up user object with numeric value
erb :home
end


I have tried enabling sessions in my config controller, that didnt work either

module Controllers

class ApplicationController < Sinatra::Base

# . . . .

configure :production, :development do
enable :sessions, :logging
end

end

end


how do I make my app persist the
session[:id]
between routes?

Answer

Have you tried enable :sessions after you require your sinatra gem in your sinatra app? Not in your controller

Comments