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
运行shotgun config.ru
或thin start -R config.ru
会产生:
app.rb:3:in `<top (required)>': undefined local variable or method `session' for main:Object (NameError)
这非常101。是什么给了什么?
答案 0 :(得分:2)
我认为问题在于您没有get
块内的代码或类似内容。基本上,sinatra只会在请求的上下文中知道session
对象。这样的东西适用于根URL:
require 'sinatra'
class MyApp < Sinatra::Application
enable :sessions
get '/' do
if session[:user_id].nil? then
erb :login
end
end
end