尝试包含自定义模块时加载错误

发布于 2024-11-19 02:35:00 字数 1634 浏览 1 评论 0原文

相同的应用程序,不同的问题。我正在开发一个应用程序,使用 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 技术交流群。

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

发布评论

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

评论(2

过期以后 2024-11-26 02:35:00

假设您使用的是 Ruby 1.9,默认的 $LOAD_PATH 不再包含当前目录。因此,虽然像 require 'sinatra' 这样的语句工作得很好(因为这些 gem 位于 $LOAD_PATH 中),但 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'

Assuming you're using Ruby 1.9, the default $LOAD_PATH no longer includes the current directory. So while statements like require 'sinatra' work just fine (because those gems are in $LOAD_PATH), Ruby doesn't know that your lib/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:

$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'
旧伤慢歌 2024-11-26 02:35:00

就个人而言,自从我使用 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 上运行会发生什么。

Personnaly, I use a "relative" path since I work with Ruby 1.9.2 :

require 'sinatra'
require 'rubygems' # Not actually needed on Ruby 1.9
require 'datamapper'
require 'dm-core'
require './lib/authorization'

But I never check what would happen if my code should work on Ruby 1.8.6 again.

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