Rails in a subdirectory

software development

#1

Is anyone running a Rails application in a subdirectory of a domain?

The wiki page says to simply make a symlink to do this, but Rails doesn’t behave properly if you do this. For example, if you redirect_to ‘login’ it will redirect to /login, not to /myapp/login


#2

I am finally able to answer my own question.

Let’s call my subdirectory /myapp. I used the “ln” command to symlink that subdirectory to my rails “public” folder.
I added this line to config/environments/production.rb:
ActionController::AbstractRequest.relative_url_root = “/myapp”

Then I had to fix anything in my application that referred to the root url as “/”. For example in config/routes.rb:
OLD: map.login ‘/login’, :controller => ‘sessions’, :action => 'new’
NEW: map.login ‘login’, :controller => ‘sessions’, :action => ‘new’

If you have redirects to “/”, you should be able to replace those with redirect_to(root_path). Refer to the output of “rake routes” for help.

The nice thing is that my development webrick server works just the same as it always did.

I’m on Rails 2.0.2


#3

Good post. This will no doubt be helpful for future reference.

Maximum Cash Discount on any plan with MAXCASH