当我发帖时,其中一个 session 变量在 IE 中丢失了。
此网络应用在 Chrome 和 Firefox 中完美运行。
这里是错误:
NoMethodError at /
undefined method `include?' for nil:NilClass
file: welcometo.rb
location: xml_file_to_use
line: 19
错误的方法如下:
def xml_file_to_use
groups = allowed_groups
#Which xml file should I write to
if (session[:location_group].include? allowed_groups[0] ) <----Line 19
......
end
这是一个解释和我的代码:
当用户登录时,设置 session 变量:
#Check to see if the user is authorized
auth_token = Adauth.authenticate(params[:user], params[:password])
if auth_token
session[:user] = params[:user]
session[:first_name] = auth_token.first_name.tr('/"[]', '')
session[:location_group] = (auth_token.groups & allowed_groups)
redirect '/'
else
redirect '/login'
end
接下来我根据这些 session 值设置变量:
get '/' do
if session["user"] != nil
@first_name = session[:first_name]
@groups = session[:groups]
@xml_file = xml_file_to_use
@location = location
erb :form
else
redirect '/login'
end
end
这部分工作得很好。我知道是因为我在我的 View 中使用了@location 变量。
定位方法如下:
#Setup Location Variable
def location
if (session[:location_group].include? allowed_groups[0] ) <-- This same code errors during a post in IE
return 'Eagan'
else
return 'Chicago'
end
end
最佳答案
Konstantin Haase 是正确的,这一定是一个错误。
从他发布的链接中,我发现将它放在我的 welcome.rb 文件的顶部可以解决问题:
set :protection, except: :session_hijacking
关于ruby - 在 Internet Explorer 上发布期间 Sinatra session 丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12220882/