如何将变量传递给 Sinatra 中的路由
How to pass a variable into a route in Sinatra
假设我有
# a bunch of methods and instance variables here
@some_var = "this is a var"
get '/' do
p @some_var
end
=> nil
# I'd like this to return "this is a var"!!!
这已经尽可能简单化了。我读过有关通过参数哈希获取变量,或使用会话在路由之间传递变量的信息
甚至
before do
@some_var = "this is a var"
end
但这不是我想要的。在我在路由中访问变量之前,我无法理解变量的范围,因为它们存在于方法中。
我错过了什么???
尝试设置:
set :foo, 'bar'
get '/foo' do
"foo is set to " + settings.foo
end
假设我有
# a bunch of methods and instance variables here
@some_var = "this is a var"
get '/' do
p @some_var
end
=> nil
# I'd like this to return "this is a var"!!!
这已经尽可能简单化了。我读过有关通过参数哈希获取变量,或使用会话在路由之间传递变量的信息 甚至
before do
@some_var = "this is a var"
end
但这不是我想要的。在我在路由中访问变量之前,我无法理解变量的范围,因为它们存在于方法中。
我错过了什么???
尝试设置:
set :foo, 'bar'
get '/foo' do
"foo is set to " + settings.foo
end