Yes! I built an application that runs many sites, doing exactly this.
The domain, or hostname, can be retrieved via:
request.env_table['HTTP_HOST']
I used a method similar to the following in application_helper.rb
def determine_site
# 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
end
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
normal locations.
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)
def include_custom_stylesheet
if File.file?("#{RAILS_ROOT}/public/stylesheets/#{@site.code}_styles.css")
'@import "/stylesheets/' + @site.code + '_styles.css";'
end
end
def display_logo
if File.file?("#{RAILS_ROOT}/public/images/logo_#{@site.code}.png")
'<img src="/images/logo_' + @site.code + '.png" border="0" alt="Logo" />'
else
# no logo found with that site code, use default
'<img src="/images/logo_default.png" border="0" alt="Logo" />'
end
end
So the layout.rhtml is basically
<html>
<head>
<style type="text/css">
@import "/stylesheets/default_styles.css";
<%= include_custom_stylesheet %>
</style>
</head>
<body>
<div><a href="/" title="Home"><%= display_logo %></a></div>
<div><%= @content_for_layout %></div>
</body>
</html>
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.