how do i know what domain is being asked for ?

lo there all.

i have a website that i am writing in rails for three different companies, each with their own domain. Each domain will need to load its own logo, and have a little different look in its CSS style sheet. However, all the same code will be run for each. What i would like to do is set something up that knows what domain is pointed at in the browser, and then load the appropriate logo and stylesheet for that domain, i suppose in a before_filter.

Anyone have an idea about how i can pull this off ?

for example domain1.com should pull domain1_style.css and domain1_logo.jpg while domain2.com should pul domain2.com_style.css and domain2_logo.jpg.

i can do this, right ?

thanks

I have also wanted to do this, but until someone pays me to solve this one it's just a dream :slight_smile:

My thoughts were to use this Buckblog: Monkey-patching Rails: Extending Routes #2, then use the subdomains call (http://api.rubyonrails.org/classes/ActionController/AbstractRequest.html#M000178) so my application could work out how to respond.

By storing all your subdomains is a table, or in the directory structure, you should be able to have each subdomain inherit the layouts/stylesheets/etc from the parent. This was there is a default is no customizations are done for the hosted company.

I have also wanted to do this, but until someone pays me to solve this one it’s just a dream :slight_smile:

My thoughts were to use this http://weblog.jamisbuck.org/2006/10/26/monkey-patching-rails-extending-routes-2, then use the subdomains call (http://api.rubyonrails.org/classes/ActionController/AbstractRequest.html#M000178 ) so my application could work out how to respond.

By storing all your subdomains is a table, or in the directory structure, you should be able to have each subdomain inherit the layouts/stylesheets/etc from the parent. This was there is a default

is no customizations are done for the hosted company.

Stepping back a little, it would probably be quite easy to just use the "subdomains" call, then base the layout and stylesheets on that. In the application controller:

find subdomain look for a directory with that name   if found? Look for a layout/stylesheet and use them or use the defaults.

i think that this is exactly what will work. using the differen tld_length so i can actually get the domain name instead of the www or whatever.

would it be just as easy to create a session variable with the domain name and then load different looks based on that ?

like domain1.com would be a :session[domain] and domain2.com would be a :session[domain]

and i could load the stylesheet based on that right from the application.rhtml.

could it be that easy ?

thanks for your time on this by the way.

sk

Not that I have tried any of this, but:

In the application controller (?) session[:domain] = request.subdomains.first

In the application.rthml

@stylesheet = File.file? session[:subdomain] || "default.css"

Something like that.....

Hey, i thought you were not going to solve this until someone paid you !

thanks for your experience on this. i will be in a position to test it out in a couple of days (when we get the new server ).

i will let you know.

thanks so much again.

shawn

Invoice is in the mail :slight_smile:

BTW - I am still learning rails, but part of my philosophy when trying to learn something is to get involved, ask questions and provide answers if I can. If I am wrong, some more experience person will say so and we all benefit.

nephish wrote:

i think that this is exactly what will work. using the differen tld_length so i can actually get the domain name instead of the www or whatever.

would it be just as easy to create a session variable with the domain name and then load different looks based on that ?

like domain1.com would be a :session[domain] and domain2.com would be a :session[domain]

and i could load the stylesheet based on that right from the application.rhtml.

could it be that easy ?

Coming in a bit late with this but yes this will work. I have used very similar approach to provide an alternative view for PDA devices. You can try the following links to get an idea.

http://www.edgesoft.ca/blog/read/2 http://mobile.edgesoft.ca/blog/read/2

Long

thanks guys, again. appreciate it a lot. shawn

Coming in a bit late with this but yes this will work.

Thanks - it's nice to get validation for your ideas :slight_smile:

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.

lo there all.

i have a website that i am writing in rails for three different companies, each with their own domain.

Each domain will need to load its own logo, and have a little different look in its CSS style sheet. However, all the same code will be run for each. What i would like to do is set something up that knows what

domain is pointed at in the browser, and then load the appropriate logo and stylesheet for that domain, i suppose in a before_filter.

Anyone have an idea about how i can pull this off ?

for example domain1.com should pull domain1_style.css and domain1_logo.jpg while domain2.com should pul domain2.com_style.css and

domain2_logo.jpg.

i can do this, right ?

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”) ‘Logo’ else # no logo found with that site code, use default ‘Logo

end end

So the layout.rhtml is basically

@import "/stylesheets/default_styles.css"; <%= include_custom_stylesheet %>
<%= @content_for_layout %>

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.

Chris Martin

Web Developer Open Source & Web Standards Advocate http://www.chriscodes.com/

shawn