Question for rails multidomain application design

Hi to all,

I would design an application for multidomain support and my question is what is the best practice to do this kind of application ?

Eg.:

I think to write my application handling the multidomain into routing map:

map.connect ":domain/:controller/:action" map.connect ":domain/:controller/:action/:id"

Assuming that I hanlde 3 domains for 3 different customers:

www.first.com www.second.com www.third.com

My customers requests could be:

http://www.first.com/public/home http://www.second.com/public/home

routed to RoR application:

/www.first.com/public/home /www.second.com/public/home /www.third.com/public/home

RoR application controller could read the home data from model finding by domain param (es. @home = Home.find_by_domain(params[:domain])

I would like to know if it is possible (and how can I solve) this kind of routing (maybe using Apache internal rewrite ?).

Thanks in advance !

Gianluca, I approached the same proble by using the domain as a unique :id and 'scoping' the entire database based on the incoming request.host. Kathy

Why not something like this instead?

class ApplicationController < ActionController::Base   before_filter :setup_application_by_domain protected   def setup_application_by_domain      @domain = Domain.find_by_name(request.domain)      # continue with your setup   end end

The mapping idea, can be a little messy, specialy if you forgot to map an action, i think it's better to get the domain information from the request object, and setup an instance of some model with all the information you need.

I haven't done this in Rails yet, but I've written several multi-domain (multi-subdomain) apps in another language, and IMO it is best to initialize an object as one of your first steps (so withing application.rb) which contains the baseline info the app needs: domain name, layout preferences, options features if applicable, etc.

So, take all those configuration details you might otherwise have in code, and push them into a db. Then the first task you do is querythe db for the config details for X domain or subdomain. From that point on your app is really like any single-domain app except that you use the domain name to identify a specific database if you use a db per domain, or it defines how you filter all db queries if you use one db for all domains.

It really is easier than it usually first sounds like.

From what I can tell, even in Rails, there's no need to map domain to a route because the domain won't change the actual actions you're calling. You want the actions themselves to adjust their behavior based on domain which is no different than having them change based on a specific logged in user. So, just like you would not change routes based on users, you don't need to do it based on domains. Make sense?

-- gw

Thanks Kathy, Pablo and Greg, u r right.

Your suggestions was very good and drive me into right direction,

I solved the problem only setting 3 virtual hosts pointing to the same rails app and simply setting the base application controller filter (no routing map at all !):

@domain = Domain.find_by_name(request.domain)

Last question:

How did u solve the different public folder for each domain ?

In my application static contents must be separated for each different domain www.first.com / www.second.com / www.third.com so I think I must have 3 different public folder, one for each different domain ?

How can handle this into rails application ? (one possible solution is to handle a parameter for images and stylesheets ":path" but I think there is a better solution because in this case I have to use tha ":path" parameter into each view...).

Thanks in advance...

Gianluca, Once you've learned to 'scope' the @domain table based on the current request.domain the rest is downhill. If I had a table called 'content' I'd use the before filter to find the domain and then gather the collection like; @content = @domain.contents.find(:all) and so on for any table in your application. Kathleen

Hi Kathleen,

thank you very much for your reply !

Thanks to your suggestion I solved the problem for accessing “dynamic” (database) contents.

The problem now is how to handle “static” contents for each different domain (eg. public/images folder on rails app is unique for all the domains and my goal is to handle static contents like images/javascripts/flash in different folders (one folder for each domain)).

One possible solution is to handle a parameter for images and stylesheets eg. @domain.path but I think there is a better solution because in this case I must set the @domain.path parameter into each view…

eg.

I would like to know if is there a better (dry) solution…

Thanks in advance.