7

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.

David Moles
  • 48,006
  • 27
  • 136
  • 235
Harry Wood
  • 2,220
  • 2
  • 24
  • 46

6 Answers6

2

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/

Joseph Yaduvanshi
  • 20,241
  • 5
  • 61
  • 69
  • Well 'compute_public_path' works although it stuffs in some 'cache busting' gubbins at the end of the path, which I don't want http://apidock.com/rails/ActionView/Helpers/AssetTagHelper/compute_public_path – Harry Wood Nov 25 '10 at 19:21
2
'<%= ENV["RAILS_RELATIVE_URL_ROOT"] %>/myfile.kml'
Harry Wood
  • 2,220
  • 2
  • 24
  • 46
2

<%= RAILS_ROOT + "/public/myfile.kml" %>

Nimesh Nikum
  • 1,809
  • 13
  • 17
1

Inspection of rake routes reveals the helper root_path for use in views. For example <%= root_path + 'myfile.kml' %> By default will map to files under public/ in a rails application.

NoelProf
  • 865
  • 7
  • 7
0

The latest (>2.3.6) is Rails.root, see: http://joneslee85.wordpress.com/2010/05/27/the-dilemma-of-rails-root-vs-rails_root-complex/

dain
  • 6,475
  • 1
  • 38
  • 47
0

Why not just replicate your production environment locally? A webserver is not very resource hungry and it can help resolve some ecosystem configuration issues like you're seeing here.

user456733
  • 351
  • 1
  • 3