views:

105

answers:

2

I would like to access one rails application through more virtual hosts. The different virtual hosts would differ in the database, images, stylesheets and in some cases the layout of the views. What do you recommend for realisation of serving multiple applications by one rails code, which is accessed through different virtual hosts?

Edited

To specify what I imagine better, I add these:

  • I prefer rails to distinguish which db/styles/images/layouts have to use based on the host name.
  • There may be different rails applications running, but above the same code.
  • Db connections might be established during the application initiation.

I am wondering which layer the rails app should recognise the host? Should it be on the middleware, in boot scripts or elsewhere?

A: 

I don't know rails but I know how I'd do it in Django. I doubt the core logic is that different.

I'd tell the server what hosts to accept (including wildcards, if I wanted) and then tell my webapp to look at the request domain (which is passed through from the webserver).

How you change various things from that point is up to you.

Oli
+1  A: 

For the database:

You can set an special parameter in the routes, like:

/:client/:controller/:action

and then use params[:client] to manually adjust ActiveRecord::Base.establish_connection(...) from an ApplicationController before_filter (I have done this an it works)

see sample code here: ruby-forum

For the stylesheets, images...: Use apache o nginx virtual host to diverge public/images and others (as yiou should do to serve the directly without rails) and choose there where you pick them from. (you may need mod_rewrite o equivalent for this)

Layouts may be a little bit more tricky, better have a common erb that loads partials wisefully depending on the params[:client].

I hope it helps.

Fer
Thanks @Fer for your answer. I do not prefer to have the client name visible in the URL. I would be happy to see rails resolving the client from the hostname. Diverging /public sounds interesting however.. I should check it out.
fifigyuri