简体   繁体   中英

How to pass a variable into a route in Sinatra

Suppose I have

# 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"!!!

This is as oversimplified as possible. I've read about getting vars through the params hash, or using sessions to pass vars between routes even a

before do
    @some_var = "this is a var"
end

But this is not what I'm after. I'm having trouble understanding the scoping of the variables as they exist in the methods before I access them in a route.

What am I missing???

Try settings:

set :foo, 'bar'

get '/foo' do
  "foo is set to " + settings.foo
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM