Ruby on Rails
HowtoUseSymLinksToGraftRailsOntoYourWebsite

WARNING: The following may not work for you due to this bug.

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.

Grafting a single application onto your site

Suppose your setup is as follows:

Then the commands you would use to create the SymLinks (under Unix-style operating system) would be:


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/

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.

Edit – Forbidden Error
If your using Confixx (or similar) and getting “403 Forbidden” try this:

move

Include /etc/apache2/httpd.conf
(or whatever you use) to the bottom of “apache2.conf”
now your .conf file is able to overwrite the settings specified in Confixx

add this code to the .conf file you included at the end of your “apache2.conf”

<Directory "/var/www/html">
  AllowOverride All
  Options +FollowSymLinks +SymLinksIfOwnerMatch


this will allow you to follow SymLinks in your DocumentRoot
(not depending on what’s set in Confixx)

Edit – RewriteBase not required

If your application uses a SymLinks 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

RewriteEngine On

insert a line reading
RewriteBase /newtech/rails/demoapp

or whatever the path from the DocumentRoot of the webserver to your app is.

If by doing this gets you to the welcome page but not to any controller and action look at your apache configuration (httpd.conf)for a line

AllowOverride None

and change it to
AllowOverride All

this will allow the .htaccess file on public to load. Or else, you could edit your httpd.conf file (if you have access). The guys at Apache recomend you not to use the .htaccess files.

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


# Include your app's configuration here:
ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS.update(
    :prefix => 
        "rails_session.#{File.dirname(__FILE__).hash.to_s(36)}."
    )

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:


# Include your app's configuration here:
ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS.update(
    :prefix =>        "rails_app1_shared_session."
    )

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.

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’ link_to and AssetHelper methods (such as image_tag). 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.