The ApplicationRoot of a single Rails application can a be placed anywhere you want it, provided only that you tie its externally visible  (URL) location to it's internal (file system) location with a [[SymLinks|Sym Link]].  

h2. Grafting a single application onto your site

Suppose your setup is as follows:

* The webserver's DocumentRoot is <code>/var/www/html/</code>
* The Rails application you wish to install is physically located in  <code>/home/ethel/public/rails/appsalot</code>
* You want the URLs to look like <code>http://www.methyethel.net/newtech/rails/demoapp/</code>

Then the commands you would use to create the [[SymLinks|Sym Link]] (under Unix-style operating system) would be:

<pre><code>
mkdir /var/www/html/newtech/
mkdir /var/www/html/newtech/rails/
ln -s /home/ethel/public/rails/appsalot/public/ /var/www/html/newtech/rails/demoapp/
</code></pre>

The first two commands simply create the directories which will hold the Sym Link.  If these directories already exist, the commands will fail harmlessly.

The third command actually makes the Sym Link.

If your application uses a [[SymLinks|Sym Link]] like this, you may ((need feedback on this!)) need to also change the .htaccess to reflect your application's public URL. Open the public/.htaccess file in your favorite text editor. After the line
<pre><code>RewriteEngine On</code></pre>
insert a line reading
<pre><code>RewriteBase /newtech/rails/demoapp</code></pre>
or whatever the path from the DocumentRoot of the webserver to your app is.

h2. Multiple applications

The technique can easily be extended to handle multiple applications on the same server; the only thing to watch out for is if two or more different rails applications use sessions and you want users to be able to visit both of them without causing a SessionRestoreError you will need to make sure their names do not conflict.  

This involves [[HowtoChangeSessionOptions|changing session options]] by editing each application's @config/environment.rb@ file to modify the prefix of the session to something unique to each application.  You could do this by hand, but we decided to use a hash of the (URL) path of the application, like so:

<pre style="overflow:scroll"><code>
# Include your app's configuration here:
ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS.update(
    :prefix => 
        "rails_session.#{File.dirname(__FILE__).hash.to_s(36)}."
    )
</code></pre> 

That way we could use the same line in @environment.rb@ for all the applications.  This would fail if we were wanting to have two copies of the _same_ application grafted on to different parts of the web site and expected them to share session data (and I suspect you'd have to be sharing something already to think that was a good idea).  In such a case we could always modify the application in question as follows:

<pre style="overflow:scroll"><code>
# Include your app's configuration here:
ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS.update(
    :prefix =>        "rails_app1_shared_session."
    )
</code></pre>

The exact text of the constant string doesn't matter, as long as it isn't used by any other rails applications on the server.

h3. Note About Links and Static Content

Links and inclusion of static content such as images, stylesheets and javascript should be made in your views using Rails' <code>link_to</code> and <code>AssetHelper</code> methods (such as <code>image_tag</code>). Static content that is linked directly using normal HTML markup will not have your application's path prefix properly appended to their src attributes, causing them to not show up in the browser.

category:Howto

