我有一个看起来像这样的 Rack 应用程序:
class Foo
def initialize(app)
@app = app
end
def call(env)
env["hello"] = "world"
@app.call(env)
end
end
将我的 Rack 应用程序连接到 Rails 后,如何env["hello"]
从 Rails 中访问?
更新:感谢 Gaius 的回答。Rack and Rails 允许您在请求期间或会话期间存储内容:
# in middleware
def call(env)
Rack::Request.new(env)["foo"] = "bar" # sticks around for one request
env["rack.session"] ||= {}
env["rack.session"]["hello"] = "world" # sticks around for duration of session
end
# in Rails
def index
if params["foo"] == "bar"
...
end
if session["hello"] == "world"
...
end
end