本文介绍了未设置的 Sinatra 和会话变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
出于某种原因,我的应用中没有设置会话变量.我使用的是 Sinatra 1.2.1.
For some reason, session variables are not being set in my app. I am using 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]
没有被设置,或者说每次请求后会话哈希都被重置.请问有人知道是怎么回事吗?
As you can see, session[:user]
is not being set, or rather the session hash is being reset after each request. Does anybody know what is going on please?
推荐答案
如果您使用的是 Shotgun,请将以下行添加到配置块中:
If you're using Shotgun, add the following line to the configure block:
set :session_secret, "My session secret"
引用 rkh,Sinatra 的当前维护者:
To quote from rkh, Sinatra's current maintainer:
[Shotgun] 将在每次请求时重新启动服务器,从而重新生成会话机密,从而使您的会话无效.这已在当前的主人中得到修复.简单修复:设置 session_secret 选项.
注意:如果您使用 Rack::Session::Pool
NOTE: This fix doesn't work if you use Rack::Session::Pool
这篇关于未设置的 Sinatra 和会话变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!