# Same controller and database, different views based on domain name

**URL:** <https://www.ruby-forum.com/t/same-controller-and-database-different-views-based-on-domain-name/182188>\
**Category:** Rails\
**Created:** [January 24, 2010, 5:26pm UTC](https://www.ruby-forum.com/t/same-controller-and-database-different-views-based-on-domain-name/182188 "2010-01-24T17:26:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![eugenio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ec9cab/32.png) [@eugenio](https://www.ruby-forum.com/u/eugenio)\
**Post date:** [January 24, 2010, 5:26pm UTC](https://www.ruby-forum.com/t/same-controller-and-database-different-views-based-on-domain-name/182188/1 "2010-01-24T17:26:44Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![eugenio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ec9cab/32.png) [@eugenio](https://www.ruby-forum.com/u/eugenio)\
**Post date:** [January 25, 2010, 12:45am UTC](https://www.ruby-forum.com/t/same-controller-and-database-different-views-based-on-domain-name/182188/2 "2010-01-25T00:45:06Z")

</div>

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  
act…

---

<div class="post-metadata">

**Author:** ![eugenio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ec9cab/32.png) [@eugenio](https://www.ruby-forum.com/u/eugenio)\
**Post date:** [January 25, 2010, 9:28am UTC](https://www.ruby-forum.com/t/same-controller-and-database-different-views-based-on-domain-name/182188/3 "2010-01-25T09:28:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![eugenio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ec9cab/32.png) [@eugenio](https://www.ruby-forum.com/u/eugenio)\
**Post date:** [January 25, 2010, 12:21pm UTC](https://www.ruby-forum.com/t/same-controller-and-database-different-views-based-on-domain-name/182188/4 "2010-01-25T12:21:34Z")

</div>

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.

(Rails::public\_path + “/themes/#{@theme}/views”)])  
end

Regards

Gary

---

<div class="post-metadata">

**Author:** ![eugenio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ec9cab/32.png) [@eugenio](https://www.ruby-forum.com/u/eugenio)\
**Post date:** [March 12, 2010, 2:58pm UTC](https://www.ruby-forum.com/t/same-controller-and-database-different-views-based-on-domain-name/182188/5 "2010-03-12T14:58:53Z")

</div>

Best solution I had seen is posted over here:  
[http://transfs.com/devblog/2009/01/21/hosting-multiple-domains-from-a-single-rails-app/](http://transfs.com/devblog/2009/01/21/hosting-multiple-domains-from-a-single-rails-app/)

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] = request.host if request.host  
env  
end  
end  
class Route  
alias\_method :old\_recognition\_conditions, :recognition\_conditions  
def recognition\_conditions  
result = old\_recognition\_conditions  
result \<\< “conditions[:domain] === env[:domain]” if  
conditions[:domain]  
result \<\< “conditions[:host] === env[:host]” if  
conditions[:host]  
result  
end  
end  
end  
end

And at the router itself:

map.connect ‘’, :controller =\> ‘true\_cost\_calculator’, :action =\> ‘new’,  
:conditions=\>{ :domain=\>‘[truecostofcredit.com](http://truecostofcredit.com)’ }  
map.connect ‘’, :controller =\> ‘another\_cost\_calculator’, :action =\>  
‘index’, :conditions=\>{ :domain=\>‘[anothercost.com](http://anothercost.com)’ }

Sometimes :host is useful too

Regards
