Generate URL for file in /public in Rails 2 ERB view

Harry Wood picture Harry Wood · Nov 25, 2010 · Viewed 12.1k times · Source

In my rails (v2.3.8) app I have a static resource file which I've put at /public/myfile.kml No need for any special routes.rb setting right?

It serves up just fine at http://localhost:3000/myfile.kml

When I deploy (to passenger) it appears at http://myserver/myappname/myfile.kml

All is well so far...

I have a view (an erb file) which spews out javascript which needs to reference this file. The output needs to be '/myfile.kml' on localhost, and '/myappname/myfile.kml' in production, or maybe the full URLs as above, or maybe a relative url involving a bit of '../../../' (awkward with RESTful URLs).

Should I be able to do something like <%=url_for 'myfile.kml'%> ?

or '<%=ROOT_URL%>/myfile.kml'

I know there's an insanely easy answer to this question, but honestly I've had no luck finding it. Quite a few people talking about 'root_url' but what is that? A variable I can reference in a view? It's undefined.

Answer

Jim Schubert picture Jim Schubert · Nov 25, 2010

I'm not sure about Rails 2.3.8, but in Rails 3 this value defaults to false.

edit config/environments/production.rb and set:

config.serve_static_assets = true

Also, here's a blog post that shows a helper for linking to a static resource (favicon) http://ilconnettivo.wordpress.com/2008/07/28/favicon-on-rails/