views:

503

answers:

2

I have apache 2.2 with mod_rails running at http://localhost. I want to have my rails app at http://localhost/railsBlog. So, what I did was, I created a virtual host:


  ServerName localhost
  DocumentRoot /Library/WebServer/Documents
  RailsEnv development
  RailsBaseURI /railsBlog

Now, since the URL is http://localhost/railsBlog, the server views railsBlog as the controller I'm passing in, which is not what I want. So when I go to http://localhost/railsBlog/home/index. This won't get to my 'home' controller and 'index' view because it tries to go to 'railsBlog' controller (doesn't exist) and 'home' view (doesn't exist).

I think one way to solve this is to redefine map.root to be /railsBlog and things should be fine. But how?

Another way I could get around this would be to modify config/routes.rb to have:

map.connect 'railsBlog/:controller/:action/:id'

However, this would mean that I would have to change this file every time I deploy to a different location.

Or, is there any other way to get around this?

+3  A: 

You can put a line like this in config/environment.rb (or one of the specific environment files)

config.action_controller.relative_url_root = "/railsBlog"

You should also symlink the publc directory to the root of the web directory, for example:

ln -s /rails/railsBlog/public /webroot/

This is all from the passenger documentation

Dan McNevin
Thanks Dan, adding that line in config/environment.rb worked!And also, I already have a symlink where http://localhost/railsBlog points to my public dir.
mixmasteralan
+1  A: 

Just to add to the previous answer... here is the url of the documentation:

http://www.modrails.com/documentation/Users%20guide.html#deploying_rails_to_sub_uri

Also, here is some information if you are running into errors with broken image, css, resource links...

http://www.modrails.com/documentation/Users%20guide.html#sub_uri_deployment_uri_fix

Basically it says you should always use the rails helper functions (image_tag, javascript_include_tag, and stylesheet_link_tag) instead of hand coding the urls. These will automatically generate the correct URL with the sub uri you have set.

This allows you to easily move the application to another sub uri or out of the sub uri configuration without changing all of your references.

One nice thing about this is that you can use one virtual server statement to deploy mutiple applications by having multiple RailsBaseURI lines. This came in handy for an app we were trying to build.

SWR