Get absolute (base) url in sinatra

berkes picture berkes · Jun 1, 2010 · Viewed 16.4k times · Source

Right now, I do a

get '/' do
  set :base_url, "#{request.env['rack.url_scheme']}://#{request.env['HTTP_HOST']}"
  # ...
  haml :index
end

to be able to use options.base_url in the HAML index.haml. But I am sure there is a far better, DRY, way of doing this. Yet I cannot see, nor find it. (I am new to Sinatra :))

Somehow, outside of get, I don't have request.env available, or so it seems. So putting it in an include did not work.

How do you get your base url?

Answer

epidemian picture epidemian · Apr 26, 2012

You can get it using request.base_url too =D (take a look at rack/request.rb)