app.rb:
require 'sinatra'
class MyApp < Sinatra::Application
enable :sessions
if session[:user_id].nil? then
erb :login
end
end
require_relative 'routes/init.rb'
config.ru:
require './app'
run MyApp
Running shotgun config.ru or thin start -R config.ru yields:
app.rb:3:in `<top (required)>': undefined local variable or method `session' for main:Object (NameError)
This is very 101ish. What gives?
I believe the problem is that you don’t have that code within a
getblock or something similar. Basically, sinatra will only know about thesessionobject in the context of a request. Something like this would work for the root url: