如何从模块访问 Sinatra Session
我正在使用 Ruby 1.9.2 和 Sinatra 1.3 开发一个应用程序。 我有一个需要访问会话数据的帮助模块。 这是我的模块的一个片段,
require 'sinatra'
module SessionHelper
def current_user
session['current_user']
end
end
这在 ruby 1.8.7 上工作得很好,但是当我使用 Ruby 1.9.2 运行该应用程序时,我收到一条错误消息: undefined local variable or method 'session' for SessionHelper:Module< /代码>
I am developing an application with Ruby 1.9.2 and Sinatra 1.3.
I have a helper module that needs to access session data.
Here is a snippet of my module
require 'sinatra'
module SessionHelper
def current_user
session['current_user']
end
end
This works fine with ruby 1.8.7, but when I run the application with Ruby 1.9.2, I get an error saying: undefined local variable or method 'session' for SessionHelper:Module
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试这样的事情:
然后在你的控制器中的某个地方,你可以这样做:
看看编写 Sinatra 的文档扩展 - 它也适用于其他自定义模块。
希望有帮助!
try something like this:
Then in your controllers somewhere, you could do this:
Take a look at the documentation on writing Sinatra extensions - it applies to other custom modules too.
Hope that helps!