Ruby 尝试包含自定义模块时发生加载错误
相同的应用程序,不同的问题。我正在开发一个应用程序,使用Dan Benjamin Meet Sinatra的屏幕广播作为参考。我试图包括一个自定义身份验证模块,它位于lib文件夹lib/authentication.rb中。我要求在我的代码顶部有这一行,但当我试图加载页面时,它声称没有这样的文件要加载 有什么想法吗 以下是我的主要Sinatra文件的顶部:Ruby 尝试包含自定义模块时发生加载错误,ruby,sinatra,basic-authentication,Ruby,Sinatra,Basic Authentication,相同的应用程序,不同的问题。我正在开发一个应用程序,使用Dan Benjamin Meet Sinatra的屏幕广播作为参考。我试图包括一个自定义身份验证模块,它位于lib文件夹lib/authentication.rb中。我要求在我的代码顶部有这一行,但当我试图加载页面时,它声称没有这样的文件要加载 有什么想法吗 以下是我的主要Sinatra文件的顶部: require 'sinatra' require 'rubygems' require 'datamapper' require 'dm-
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放入其中。假设您使用的是Ruby 1.9,默认的$LOAD_路径不再包括当前目录。因此,虽然像require'sinatra'这样的语句可以正常工作,因为这些gem位于$LOAD_路径中,但Ruby不知道您的lib/authorization文件相对于主sinatra文件的位置 您可以将Sinatra文件的目录添加到加载路径,然后require语句应该可以正常工作:
$LOAD_PATH.unshift(File.dirname(__FILE__))
require 'sinatra'
require 'rubygems' # Not actually needed on Ruby 1.9
require 'datamapper'
require 'dm-core'
require 'lib/authorization'
假设您使用的是Ruby 1.9,默认的$LOAD_路径不再包括当前目录。因此,虽然像require'sinatra'这样的语句可以正常工作,因为这些gem位于$LOAD_路径中,但Ruby不知道您的lib/authorization文件相对于主sinatra文件的位置 您可以将Sinatra文件的目录添加到加载路径,然后require语句应该可以正常工作:
$LOAD_PATH.unshift(File.dirname(__FILE__))
require 'sinatra'
require 'rubygems' # Not actually needed on Ruby 1.9
require 'datamapper'
require 'dm-core'
require 'lib/authorization'
个人而言,我使用相对路径,因为我使用Ruby 1.9.2:
require 'sinatra'
require 'rubygems' # Not actually needed on Ruby 1.9
require 'datamapper'
require 'dm-core'
require './lib/authorization'
但我从不检查如果我的代码再次在Ruby 1.8.6上运行会发生什么情况。个人而言,我使用相对路径,因为我使用Ruby 1.9.2:
require 'sinatra'
require 'rubygems' # Not actually needed on Ruby 1.9
require 'datamapper'
require 'dm-core'
require './lib/authorization'
但是我从不检查如果我的代码再次在Ruby 1.8.6上运行会发生什么情况。您能发布您尝试加载该文件时使用的确切代码吗?在文本结尾和代码开头之间添加了一个空行,这样,您的所有代码都将格式化为代码。您可以发布您尝试加载该文件时使用的确切代码吗?在文本的结尾和代码的开头之间添加一个空行,以便将您的所有代码格式化为代码。这样做了。我想我下面的教程是在ruby 1.8中完成的。非常感谢你!成功了。我想我下面的教程是在ruby 1.8中完成的。非常感谢你!在我看来,这个答案应该是可以接受的。到目前为止,我一直在使用hack来取消加载路径,但它对我来说总是很难看,这就是我一直在寻找的解决方案:我喜欢这个选项,因为它需要更少的代码。但这两种选择都有效!在我看来,这个答案应该是可以接受的。到目前为止,我一直在使用hack来取消加载路径,但它对我来说总是很难看,这就是我一直在寻找的解决方案:我喜欢这个选项,因为它需要更少的代码。但这两种选择都有效!