1

Is there a way to refer to the root URL of my Sinatra app? Say in one of the views I'd like to do the following:

<a href="<%= ROOT_PATH/cats %>">Show all cats</a>

Does Sinatra provide a magic helper for ROOT_PATH or it's equivalent?

sakovias
  • 1,356
  • 1
  • 17
  • 26

2 Answers2

4

It should be accessible through request.base_url

Which is implemented by rack using:

def base_url
  url = "#{scheme}://#{host}"
  url << ":#{port}" if port != DEFAULT_PORTS[scheme]
  url
end

Get absolute (base) url in sinatra

Community
  • 1
  • 1
Travis
  • 13,311
  • 4
  • 26
  • 40
2

There is also the uri helper

#uri(addr = nil, absolute = true, add_script_name = true) ⇒ Object  

Also known as: url, to

Generates the absolute URI for a given path in the app. Takes Rack routers and reverse proxies into account.

For your example:

<a href="<%= uri('/cats') %>">Show all cats</a>
ian
  • 12,003
  • 9
  • 51
  • 107