sakovias
sakovias

Reputation: 1434

How to refer to root path in Sinatra views

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?

Upvotes: 1

Views: 1680

Answers (2)

ian
ian

Reputation: 12251

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>

Upvotes: 2

Travis
Travis

Reputation: 14436

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

Upvotes: 4

Related Questions