[英]How do I set/get session vars in a Rack app?
use Rack::Session::Pool
...
session[:msg]="Hello Rack"
EDIT: The word session doesn't seem to resolve. 编辑:单词会话似乎没有解决。 I included the Session pool middleware in my config.ru, and try to set a variable in an ERB file (I'm using Ruby Serve) and it complains "undefined local variable or method `session'"
我在我的config.ru中包含了Session pool中间件,并尝试在ERB文件中设置一个变量(我正在使用Ruby Serve)并且它抱怨“未定义的局部变量或方法`session'”
Thanks! 谢谢!
session
is a method that is part of some web frameworks, for example Sinatra and Rails both have session
methods. session
是一种方法,是某些Web框架的一部分,例如Sinatra和Rails都有session
方法。 Plain rack
applications don't have a session
method, unless you add one yourself. 普通
rack
应用程序没有session
方法,除非您自己添加。
The session hash is stored in the rack env hash under the key rack.session
, so you can access it like this (assuming you've named the rack environment to your app env
): 会议散列存储在项下架ENV哈希
rack.session
,这样你就可以像这样(假设你已经命名机架环境,你的应用程序访问它env
):
env['rack.session'][:msg]="Hello Rack"
Alternatively, you could use Rack's built in request
object , like this: 或者,您可以使用Rack的内置
request
对象 ,如下所示:
request = Rack::Request.new(env)
request.session[:msg]="Hello Rack"
You need to load rack::session
module next probably cookie like here http://rack.rubyforge.org/doc/classes/Rack/Session/Cookie.html 你需要加载
rack::session
module下一个可能的cookie,如http://rack.rubyforge.org/doc/classes/Rack/Session/Cookie.html
This like explains it with example. 这就像用例子解释一样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.