如何从模块访问 Sinatra Session

发布于 2024-12-21 03:00:09 字数 369 浏览 1 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寒冷纷飞旳雪 2024-12-28 03:00:09

尝试这样的事情:

require 'sinatra/base'

module Sinatra
  module SessionHelper
    def current_user
      session['current_user']
    end
  end
  register current_user
end

然后在你的控制器中的某个地方,你可以这样做:

user = current_user

看看编写 Sinatra 的文档扩展 - 它也适用于其他自定义模块。

希望有帮助!

try something like this:

require 'sinatra/base'

module Sinatra
  module SessionHelper
    def current_user
      session['current_user']
    end
  end
  register current_user
end

Then in your controllers somewhere, you could do this:

user = current_user

Take a look at the documentation on writing Sinatra extensions - it applies to other custom modules too.

Hope that helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文