出于某种原因,我的应用程序中没有设置会话变量。我正在使用 Sinatra 1.2.1。
这是一段代码:
module GitWiki
class App < Sinatra::Base
configure do
enable :sessions
set :app_file, __FILE__
set :root, File.expand_path(File.join(File.dirname(__FILE__), '..', '..'))
set :auth do |bool|
condition do
redirect '/login' unless logged_in?
end
end
end
helpers do
def logged_in?
not @user.nil?
end
end
error PageNotFound do
page = request.env["sinatra.error"].name
redirect "/#{page}/edit"
end
before do
content_type "text/html", :charset => "utf-8"
@user = session[:user]
end
get "/login/?" do
erb :login
end
post "/login" do
user = User.get
if user.authenticate(params[:username], params[:password])
session[:user] = params[:username]
p session # => {:user=>"root"}
else
# AZIZ! LIGHT!
end
redirect '/'
end
get "/" do
p session # => {}
redirect "/" + GitWiki.homepage
end
# ...
end
end
如您所见,session[:user]
没有设置,或者更确切地说,会话哈希在每次请求后被重置。请问有人知道发生了什么吗?