Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 508
Harry Wood Avatar asked Nov 25 '10 18:11

Harry Wood


2 Answers

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/

like image 97
Jim Schubert Avatar answered Nov 29 '22 20:11

Jim Schubert


'<%= ENV["RAILS_RELATIVE_URL_ROOT"] %>/myfile.kml'
like image 25
Harry Wood Avatar answered Nov 29 '22 19:11

Harry Wood