# Best practice for running an app in subdirectory

**URL:** <https://www.ruby-forum.com/t/best-practice-for-running-an-app-in-subdirectory/181304>\
**Category:** Rails\
**Created:** [January 9, 2010, 6:37am UTC](https://www.ruby-forum.com/t/best-practice-for-running-an-app-in-subdirectory/181304 "2010-01-09T06:37:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![coolmenu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eada6e/32.png) [@coolmenu](https://www.ruby-forum.com/u/coolmenu)\
**Post date:** [January 9, 2010, 6:37am UTC](https://www.ruby-forum.com/t/best-practice-for-running-an-app-in-subdirectory/181304/1 "2010-01-09T06:37:16Z")

</div>

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 [www.domain.com/myapp](http://www.domain.com/myapp) (for example).

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

And of course that doesn’t work…it 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:

config.action\_controller.relative\_url\_root

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):

RAILS\_RELATIVE\_URL\_ROOT = ‘/myapp’

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

And same thing with an image:

![]()

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!

---

<div class="post-metadata">

**Author:** ![coolmenu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eada6e/32.png) [@coolmenu](https://www.ruby-forum.com/u/coolmenu)\
**Post date:** [January 10, 2010, 4:57am UTC](https://www.ruby-forum.com/t/best-practice-for-running-an-app-in-subdirectory/181304/2 "2010-01-10T04:57:24Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![coolmenu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eada6e/32.png) [@coolmenu](https://www.ruby-forum.com/u/coolmenu)\
**Post date:** [January 10, 2010, 6:06am UTC](https://www.ruby-forum.com/t/best-practice-for-running-an-app-in-subdirectory/181304/3 "2010-01-10T06:06:27Z")

</div>

On Jan 9, 10:55Â pm, Lee S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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.

---

<div class="post-metadata">

**Author:** ![coolmenu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eada6e/32.png) [@coolmenu](https://www.ruby-forum.com/u/coolmenu)\
**Post date:** [January 10, 2010, 6:52am UTC](https://www.ruby-forum.com/t/best-practice-for-running-an-app-in-subdirectory/181304/4 "2010-01-10T06:52:40Z")

</div>

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.
