Route to static file in Play! 2.0

Nitzan Tomer picture Nitzan Tomer · Mar 20, 2012 · Viewed 27.2k times · Source

I'm trying to make a route to a specific static file but everything I'm trying ends with an error.

I've made 3 different attempts:

1.

GET /file   staticFile:/public/html/file.html

The error I get:

Compilation error
string matching regex `\z' expected but `:' found

2.

GET /file   controllers.Assets.at(path="/public/html", "file.html")

The error I get:

Compilation error
Identifier expected

3.

GET /file   controllers.Assets.at(path="/public/html", file="file.html")

The error I get: (and this is the weirdest)

Compilation error
not enough arguments for method at: (path: String, file: String)play.api.mvc.Call. Unspecified value parameter file.

The weird part about the 3rd error is that it's thrown in a different file (app/views/main.scala.html) on the following line:

<link rel="stylesheet" media="screen" href="@routes.Assets.at("stylesheets/main.css")">

All of these methods were found in the official documentation and/or threads here on stackoverflow. What am I missing here?

Thanks.

Answer

Jamil picture Jamil · Mar 21, 2012

IIRC, change

<link rel="stylesheet" media="screen" href="@routes.Assets.at("stylesheets/main.css")">

To

<link rel="stylesheet" media="screen" href="@routes.Assets.at("stylesheets/", "main.css")">

I am talking about your third attempt

Also, watch out for extra /

EDIT

GET /assets/main.css    controllers.Assets.at(path="/public", file="/stylesheets/main.css")

Assuming your resource is at /public/stylesheets/main.css