Search code examples
ruby-on-rails-2

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


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.


Solution

  • '<%= ENV["RAILS_RELATIVE_URL_ROOT"] %>/myfile.kml'