Sergei Stralenia Sergei Stralenia - 1 year ago 107
HTML Question

Martini routes with named parameter fails static files loading

I wrote my first Go application with Martini. I have route with named parameter:

m := martini.Classic()

staticOptions := martini.StaticOptions{Prefix: "assets"}
m.Use(martini.Static("assets", staticOptions))

m.Get("/edit/:id", editHandler)

The editHandler renders edit template with bootstrap stylesheet and script which lie in
folders accordingly.

<link rel="stylesheet" type="text/css" href="assets/css/bootstrap.min.css">
<script type="text/javascript" src="assets/js/bootstrap.min.js"></script>

But when I open edit page in my browser my static files don't load, because browser makes requests to
How can I strip any route prefix?('edit', 'show' and others)

Answer Source

The links in your supplied html snippet are relative links. This means that when the browser resolves the URL it appends the given path to the current host and path ie, http://<hostname_and_port>/edit + assets/css/bootstrap.min.css.

You can use a root path instead of a relative path to ensure that when the browser resolves urls it appends the given path to the root of the host no matter what the full URL path is. This is done by putting a forward slash at the beginning of your path. In the following example I have added a forward slash to the src and href attribute paths.

<link rel="stylesheet" type="text/css" href="/assets/css/bootstrap.min.css">
<script type="text/javascript" src="/assets/js/bootstrap.min.js"></script>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download