Same controller and database, different views based on domain name

what is the easiest way to achieve that result?
i would like to keep controllers and models the same for all the
“applications”, which, in fact, are the same. They only appear
different (the views, the css, and a parameter of the query that will
be sent to the database).
I could separate the rails app, but, doing that, i will need to
replicate every common change (a bug fix in a model method for
example) to all of them. is there a better way?

I think it is rather problem of solving question how to configure
virtualHost not rather rails app itself…

It is easy for instance with Passenger and apache, you configure
virtualHost’s files for appropriate domains and pass on every same
home folder of your app to them

In app core you see what is your request url and you know how to

Why not just grab the domain from the URL, then use that to determine
which stylesheet, etc. to use?

You have a couple of options.

  1. Setup virtual hosts on your web server and find a way to keep the
    public folders seperate(easy in passenger on apache, but cannot speak
    for anything else). This does not allow for having seperate view
    files for each subdomain.

  2. In addition to or instead of 1, use the code below in your
    application controller and call it before every request with the theme
    for that request (i.e. get it from the params in one way or another -
    I use a database lookup as my sites are defined in the db, but that is
    not nescessary unless you require it for another reason).
    Note that the code below will expect the different views in a folder
    called ‘themes/<>/views’ - change this to wherever you want to
    store it.
    The good thing about this method is that it uses rails to do the
    clever stuff and if the view file that you want isn’t in the folder
    specified, then it will look in existing locations i.e. your
    application. This is useful if you want all of your subdomains to
    keep the same view files as the app UNLESS you want to change them for
    that subdomain.

       def set_theme(theme)

(Rails::public_path + “/themes/#{@theme}/views”)])



Best solution I had seen is posted over here:

I was developing recently such application and it works perfectly with
Rails 2.3.5

Besides of configuring VirtualHost, everything focuses on extending
functionality of routing. So in new initializer you put:

module ActionController
module Routing
class RouteSet
def extract_request_environment(request)
env = { :method => request.method }
env[:domain] = request.domain if request.domain
env[:host] = if
class Route
alias_method :old_recognition_conditions, :recognition_conditions
def recognition_conditions
result = old_recognition_conditions
result << “conditions[:domain] === env[:domain]” if
result << “conditions[:host] === env[:host]” if

And at the router itself:

map.connect ‘’, :controller => ‘true_cost_calculator’, :action => ‘new’,
:conditions=>{ :domain=>‘’ }
map.connect ‘’, :controller => ‘another_cost_calculator’, :action =>
‘index’, :conditions=>{ :domain=>‘’ }

Sometimes :host is useful too


This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs