Creating a URL for a file in / public in Rails 2 ERB mode

In my rails application (v2.3.8), I have a static resource file that I put in /public/myfile.kml. Is route.rb correct for any special settings?

It is great for http: // localhost: 3000 / myfile.kml

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

Everything is fine so far ...

I have a view (erb file) that outputs javascript that should reference this file. The output should be "/myfile.kml" on localhost and "/myappname/myfile.kml" during production, or possibly the full URL as above, or perhaps the relative URL containing the bit ".. /../../'(comfortable with RESTful URLs).

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

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

I know there is an insanely easy answer to this question, but honestly, I was not lucky to find it. Quite a lot of people talk about root_url, but what is it? A variable that I can refer to in a view? It is undefined.

+7
ruby-on-rails-2
source share
6 answers
'<%= ENV["RAILS_RELATIVE_URL_ROOT"] %>/myfile.kml' 
+2
source share

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

edit config/environments/production.rb and install:

config.serve_static_assets = true

In addition, here is a blog post showing a helper for linking to a static resource (favicon) http://ilconnettivo.wordpress.com/2008/07/28/favicon-on-rails/

+2
source share

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

+2
source share

The rake routes check shows an auxiliary root_path for use in views. For example, <%= root_path + 'myfile.kml' %> By default, the file will be displayed in the public / in the rails application.

+1
source share
0
source share

Why not just localize your production environment? The web server is not very resource intensive, and it can help solve some of the ecosystem configuration problems that you see here.

0
source share

All Articles