Best practice for running an app in subdirectory

I seem to be misunderstanding something very basic in trying to run my app in a subdirectory. Remarkably, all my restful routes work flawlessly. Everything outside of rails routing conventions is what's giving me problems. My app is using mod_rails and I've got it sym- linked to (for example).

My application layout includes this line in the head section of the html:

<link rel="shortcut icon" type="image/ico" href="/favicon.ico" />

And of course that doesn't can't find my app's favicon, instead pointing to the root of my domain to look for it. Basically, you can't reference anything with a leading slash if you're running the app in a subdirectory. Manual routing like this needs to know the "url root", I'm thinking. So I found this setting:


Rails guides says that it can be used to tell Rails that you are deploying to a subdirectory. The default is ENV ['RAILS_RELATIVE_URL_ROOT']. Ok, so I added that config setting to my config/environment.rb and nothing changed. What exactly does this action_controller setting do and how do you use it?

The other thing I tried (which worked but it's ugly) was to set the RAILS_RELATIVE_URL_ROOT env variable in config/environment.rb to my app's root (/myapp):


Then in my application layout, I accessed the app's favicon like this:

<link rel="shortcut icon" type="image/ico" href="< %=RAILS_RELATIVE_URL_ROOT%>/favicon.ico" />

And same thing with an image:

<img src="<%=RAILS_RELATIVE_URL_ROOT%>/images/avatar.gif" alt="" style="border: 1px solid #ddd; padding: 3px; margin: 0 10px 0 0;"/>

So everywhere I need to reference a path outside of rails' named routes, I have to insert this env variable? That can't be best practice! Or is it?

Thanks for any help!

Well the image issue is easily corrected...use the view helper for image tags:

<%=image_tag("avatar.gif", :alt => "", :style => "border: 1px solid #ddd; padding: 3px; margin: 0 10px 0 0;")%>

That way I'm able to let Rails handle the application pathing just like the restful routes...and no mention of any env variable.

But that won't work for the favicon which sits in public/ so I'm still tied down to having to include that env variable in the view like that?

And the question I have about the action controller relative_url_root config option...what does that do exactly and how do I use it? Or do I need to use it? My app is sitting in a subdirectory and it's working without that settting (I'm still declaring RAILS_RELATIVE_URL_ROOT though to get the favicon working).

Thanks for any help.

relative_url_root just.... refers to the app's relative url root. It's prepended to the path returned by in javascript_include_tag, image_tag, and such. You can access this variable in your helpers/ whatever via ActionController::Base.relative_url_root.

Thanks pharrington. Whenever I need explicit access to the relative root, I call a helper method in my application_helper to return that config variable. That's exactly what I needed. Thanks again.