Sinatra 授权扩展:NameError

发布于 2024-11-16 17:33:30 字数 1105 浏览 6 评论 0原文

谁能告诉我为什么在调用 /admin 路由时出现 NameError - uninitializedconstant Sinatra::Default

require "sinatra"
require "sinatra/authorization"

set :authorization_realm, "Protected zone"

helpers do
  def authorize(login, password)
    login == "admin" && password == "secret"
  end
end

get "/" do
  "Welcome to the public zone"
end

get "/admin" do
  login_required
  "Welcome to the protected zone"
end

我使用的是 Ruby 1.9.2、Sinatra 1.2.6 和 Sinatra 授权扩展

更新:好的,我在以下文件的第 25 行找到了问题的根源:

sinatra-authorization-1.0.0/lib/sinatra/authorization.rb

原始项目的这个分支通过替换解决了问题Sinatra::Default.authorization_realmsettings.authorization_realm 在第 25 行。这解决了问题,但我不明白为什么。谁能向我解释一下吗?

Can anyone tell me why I get a NameError - uninitialized constant Sinatra::Default when calling the /admin route?

require "sinatra"
require "sinatra/authorization"

set :authorization_realm, "Protected zone"

helpers do
  def authorize(login, password)
    login == "admin" && password == "secret"
  end
end

get "/" do
  "Welcome to the public zone"
end

get "/admin" do
  login_required
  "Welcome to the protected zone"
end

I'm using Ruby 1.9.2, Sinatra 1.2.6 and the Sinatra Authorization Extension.

UPDATE: OK, I've found the source of the problem at line 25 of the following file:

sinatra-authorization-1.0.0/lib/sinatra/authorization.rb

This fork of the original project solved the problem by replacing Sinatra::Default.authorization_realm with settings.authorization_realm at line 25. This solves the problem, but I don't understand why. Can anyone explain that to me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

心如荒岛 2024-11-23 17:33:30

它解决了问题,因为 Sinatra 不再有 Default 类。如果您想访问使用 :set 定义的某些设置,您应该使用 settings.

It's solves the problem because Sinatra doesn't have Default class anymore. And if you want to access some settings defined with :set you should use settings. for this.

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