Yes! I built an application that runs many sites, doing exactly this.
The domain, or hostname, can be retrieved via:
I used a method similar to the following in application_helper.rb
# search for domain in sites table
@site = Site.find_by_domain(request.env_table['HTTP_HOST'])
# get default site if no site was found
@site = Site.find_by_domain("www.defaultdomain.com") if !@site
There's obviously a 'sites' table in the database that stores the
domain name, amongst other things.
'determine_site' is called in each controller action (this is what
could be more elegant), but it returns info relating to the site
including a field named 'code' that I used to bring up the correct
stylesheets and logos. The css and logo files are named using the
site.code. This way I could keep all logos and stylesheets in the
You could simply use the @site.code in the image and stylesheet html
in your layout.
I chose to be sure the files actually exist before calling them, and
keep my layout file pretty by defining and calling the following
methods (also in application_helper.rb)
'@import "/stylesheets/' + @site.code + '_styles.css";'
'<img src="/images/logo_' + @site.code + '.png" border="0" alt="Logo" />'
# no logo found with that site code, use default
'<img src="/images/logo_default.png" border="0" alt="Logo" />'
So the layout.rhtml is basically
<%= include_custom_stylesheet %>
<div><a href="/" title="Home"><%= display_logo %></a></div>
<div><%= @content_for_layout %></div>
Note that you could simply return the default CSS file in the
'include_custom_stylesheet' method, as done in 'display_logo'. Given
the nature of CSS, it was simpler (and DRY'er in my CSS) for me to
just change what was necessary (as changes were minimal) and let the
"cascading" part of CSS do the magic of overriding the necessary
default styles. YMMV
You could also just call the logos from CSS too and avoid the logo
trick all together, but that may or may not work in your situation.
There are definitely other ways of doing this, but this fit my
requirements fine so I went with it.
Hope that helps.