gpt4 book ai didi

ruby - 哈希的未定义方法 `to_i'

转载 作者:太空宇宙 更新时间:2023-11-03 16:05:07 25 4
gpt4 key购买 nike

我正在尝试为我的 Rack 应用程序创建一个子域路由器,但我遇到了一个问题。这是我的 config.ru 代码:

require './controllers/subdomain'
require './controllers/www'

set :root, './'

run Example::Subdomain.new({
:www => Sinatra::Application
})

这是我的 subdomain.rb 代码:

module Example
class Subdomain
def initialize(map = {})
@map = map
end

def call(env)
@map.each do |subdomain, app|
if env['HTTP_HOST'].split('.').first.eql?(subdomain)
app.call(env)
end
end
end
end
end

当我运行它时,出现以下错误:

NoMethodError: undefined method `to_i' for {:www=>Sinatra::Application}:Hash
/Library/Ruby/Gems/1.8/gems/rack-1.5.2/lib/rack/lint.rb:555:in `check_status'
/Library/Ruby/Gems/1.8/gems/rack-1.5.2/lib/rack/lint.rb:19:in `call'
/Library/Ruby/Gems/1.8/gems/rack-1.5.2/lib/rack/lint.rb:19:in `assert'
/Library/Ruby/Gems/1.8/gems/rack-1.5.2/lib/rack/lint.rb:555:in `check_status'
/Library/Ruby/Gems/1.8/gems/rack-1.5.2/lib/rack/lint.rb:51:in `_call'
/Library/Ruby/Gems/1.8/gems/rack-1.5.2/lib/rack/lint.rb:37:in `call'
/Library/Ruby/Gems/1.8/gems/rack-1.5.2/lib/rack/showexceptions.rb:24:in `call'
/Library/Ruby/Gems/1.8/gems/rack-1.5.2/lib/rack/commonlogger.rb:33:in `call_without_check'
/Library/Ruby/Gems/1.8/gems/sinatra-1.3.6/lib/sinatra/base.rb:161:in `call'
/Library/Ruby/Gems/1.8/gems/rack-1.5.2/lib/rack/chunked.rb:43:in `call'
/Library/Ruby/Gems/1.8/gems/rack-1.5.2/lib/rack/content_length.rb:14:in `call'
/Library/Ruby/Gems/1.8/gems/thin-1.5.0/lib/thin/connection.rb:81:in `pre_process'
/Library/Ruby/Gems/1.8/gems/thin-1.5.0/lib/thin/connection.rb:79:in `catch'
/Library/Ruby/Gems/1.8/gems/thin-1.5.0/lib/thin/connection.rb:79:in `pre_process'
/Library/Ruby/Gems/1.8/gems/thin-1.5.0/lib/thin/connection.rb:54:in `process'
/Library/Ruby/Gems/1.8/gems/thin-1.5.0/lib/thin/connection.rb:39:in `receive_data'
/Library/Ruby/Gems/1.8/gems/eventmachine-1.0.3/lib/eventmachine.rb:187:in `run_machine'
/Library/Ruby/Gems/1.8/gems/eventmachine-1.0.3/lib/eventmachine.rb:187:in `run'
/Library/Ruby/Gems/1.8/gems/thin-1.5.0/lib/thin/backends/base.rb:63:in `start'
/Library/Ruby/Gems/1.8/gems/thin-1.5.0/lib/thin/server.rb:159:in `start'
/Library/Ruby/Gems/1.8/gems/rack-1.5.2/lib/rack/handler/thin.rb:16:in `run'
/Library/Ruby/Gems/1.8/gems/rack-1.5.2/lib/rack/server.rb:264:in `start'
/Library/Ruby/Gems/1.8/gems/rack-1.5.2/lib/rack/server.rb:141:in `start'
/Library/Ruby/Gems/1.8/gems/rack-1.5.2/bin/rackup:4
/usr/bin/rackup:19:in `load'
/usr/bin/rackup:19
-e:1:in `load'
-e:1

有没有人知道为什么会发生这种情况而不是运行 app.call(env)

这是错误的截图: http://i46.tinypic.com/3538is6.jpg

此代码也有效,但它不遵守子域规则:

module Example
class Subdomain
def initialize(map = {})
@map = map
end

def call(env)
@map.each do |subdomain, app|
return app.call(env)
end
end
end
end

最佳答案

Rack 认为您正在为响应的三元组中的状态代码返回 @map,即 [status, headers, body](如 here 所示) ).这是因为 each 在其迭代结束时返回集合,并且它被用作您的 call 的返回值。

也许这对您有用,或者至少让您入门:

def call(env)
subdomain = env['HTTP_HOST'].split('.').first
app = @map[subdomain]
app.call(env)
end

关于ruby - 哈希的未定义方法 `to_i',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15582080/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com