尝试包含自定义模块时加载错误
相同的应用程序,不同的问题。我正在开发一个应用程序,使用 Dan Benjamin“Meet Sinatra”截屏视频作为参考。我试图包含一个自定义身份验证模块,该模块位于 lib 文件夹(lib/authentication.rb)中。我需要在代码顶部添加该行,但是当我尝试加载页面时,它声称没有要加载的文件。
有什么想法吗?
这是我的主 Sinatra 文件的顶部:
require 'sinatra'
require 'rubygems'
require 'datamapper'
require 'dm-core'
require 'lib/authorization'
DataMapper::setup(:default, "sqlite3://#{Dir.pwd}/entries.db")
class Entry
include DataMapper::Resource
property :id, Serial
property :first_name, String
property :last_name, String
property :email, String
property :created_at, DateTime
end
# create, upgrade, or migrate tables automatically
DataMapper.auto_upgrade!
helpers do
include Sinatra::Authorization
end
和实际的模块:
module Sinatra
module Authorization
def auth
@auth ||= Rack::Auth::Basic::Request.new(request.env)
end
def unauthorized!(realm="Short URL Generator")
headers 'WWW-Authenticate' => %(Basic realm="#{realm}")
throw :halt, [ 401, 'Authorization Required' ]
end
def bad_request!
throw :halt, [ 400, 'Bad Request' ]
end
def authorized?
request.env['REMOTE_USER']
end
def authorize(username, password)
if (username=='topfunky' && password=='peepcode') then
true
else
false
end
end
def require_admin
return if authorized?
unauthorized! unless auth.provided?
bad_request! unless auth.basic?
unauthorized! unless authorize(*auth.credentials)
request.env['REMOTE_USER'] = auth.username
end
def admin?
authorized?
end
end
end
然后,在我想要保护的任何处理程序上,我输入“require_admin”。
Same app, different problem. I'm working on an app using the Dan Benjamin "Meet Sinatra" screencast as a reference. I'm trying to include a custom authentication module, which is housed in a lib folder (lib/authentication.rb). I am requiring that line at the top of my code, but when I try to load the page, it claims there is no such file to load.
Any thoughts?
Here's the top of my main Sinatra file:
require 'sinatra'
require 'rubygems'
require 'datamapper'
require 'dm-core'
require 'lib/authorization'
DataMapper::setup(:default, "sqlite3://#{Dir.pwd}/entries.db")
class Entry
include DataMapper::Resource
property :id, Serial
property :first_name, String
property :last_name, String
property :email, String
property :created_at, DateTime
end
# create, upgrade, or migrate tables automatically
DataMapper.auto_upgrade!
helpers do
include Sinatra::Authorization
end
And the actual Module:
module Sinatra
module Authorization
def auth
@auth ||= Rack::Auth::Basic::Request.new(request.env)
end
def unauthorized!(realm="Short URL Generator")
headers 'WWW-Authenticate' => %(Basic realm="#{realm}")
throw :halt, [ 401, 'Authorization Required' ]
end
def bad_request!
throw :halt, [ 400, 'Bad Request' ]
end
def authorized?
request.env['REMOTE_USER']
end
def authorize(username, password)
if (username=='topfunky' && password=='peepcode') then
true
else
false
end
end
def require_admin
return if authorized?
unauthorized! unless auth.provided?
bad_request! unless auth.basic?
unauthorized! unless authorize(*auth.credentials)
request.env['REMOTE_USER'] = auth.username
end
def admin?
authorized?
end
end
end
Then, on any of the handlers I want to protect, I put "require_admin."
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设您使用的是 Ruby 1.9,默认的
$LOAD_PATH
不再包含当前目录。因此,虽然像require 'sinatra'
这样的语句工作得很好(因为这些 gem 位于$LOAD_PATH
中),但 Ruby 并不知道您的lib/authorization 文件位于相对于您的主 Sinatra 文件的位置。
您可以将 Sinatra 文件的目录添加到加载路径,然后您的
require
语句应该可以正常工作:Assuming you're using Ruby 1.9, the default
$LOAD_PATH
no longer includes the current directory. So while statements likerequire 'sinatra'
work just fine (because those gems are in$LOAD_PATH
), Ruby doesn't know that yourlib/authorization
file is located relative to your main Sinatra file.You can add the Sinatra file's directory to the load path, and then your
require
statements should work fine:就个人而言,自从我使用 Ruby 1.9.2 以来,我就使用“相对”路径:
但我从不检查如果我的代码再次在 Ruby 1.8.6 上运行会发生什么。
Personnaly, I use a "relative" path since I work with Ruby 1.9.2 :
But I never check what would happen if my code should work on Ruby 1.8.6 again.