使用 sinatra 在块之间传递数据
我正在尝试使用 sinatra 在块之间传递数据。例如:
@data = Hash.new
post "/" do
@data[:test] = params.fetch("test").to_s
redirect "/tmp"
end
get "/tmp" do
puts @data[:test]
end
但是,每当我到达 tmp 块时,@data 就是 nil 并抛出错误。这是为什么?
I'm trying to pass data between blocks using sinatra. For example:
@data = Hash.new
post "/" do
@data[:test] = params.fetch("test").to_s
redirect "/tmp"
end
get "/tmp" do
puts @data[:test]
end
However whenever i get to the tmp block @data is nil and throws an error. Why is that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
原因是浏览器实际上执行了两个单独的 HTTP 请求。
两个请求意味着两个单独的进程,因此一旦发送第一个响应,@data 实例变量就会被清除。
如果要保留信息,则需要使用cookie或session,否则在querystring中传递数据
The reason is because the browser actually performs two separate HTTP requests.
Two requests means two separate processes thus the @data instance variable is cleared once the first response is sent.
If you want to preserve the information, you need to use cookies or sessions, otherwise pass the data in querystring