我在他的Tekpub / Rails 3教程视频中关注Rob Conery。我认为在视频版本(1.1)和我的机器(1.4.5)上的版本之间发生了一些变化。我不知道如何弄清楚我需要采取哪些不同的行动。
在下面的代码中,我有一个字符串变量out
,并尝试将字符串变量(由MyApp.Call
方法返回的数组的第三个元素)连接到它上。
class EnvironmentOutput
def initialize(app)
@app = app
end
def call(env)
out = ""
unless(@app.nil?)
response = @app.call(env)[2]
# The Problem is HERE (Can't Convert Array to String):
out+=response
end
env.keys.each {|key| out+="<li>#{key}=#{env[key]}"}
["200", {"Content-Type" => "text/html"}, [out]]
end
end
class MyApp
def call(env)
["200", {"Content-Type" => "text/html"}, ["<h1>Hello There</h1>"]]
end
end
use EnvironmentOutput
run MyApp.new
但我收到错误:
"Can't Convert Array to String" at `out+=response`
我在这里缺少什么?
答案 0 :(得分:2)
您正在尝试将字符串添加到数组中。
的第三个要素["200", {"Content-Type" => "text/html"}, ["<h1>Hello There</h1>"]] is ["<h1>Hello There</h1>"]
是一个数组。
您可以将该数组更改为带有join的字符串:
response = @app.call(env)[2].join