# Subdomains with Rails 2.2

**URL:** <https://www.ruby-forum.com/t/subdomains-with-rails-2-2/154758>\
**Category:** Rails\
**Created:** [December 3, 2008, 5:19pm UTC](https://www.ruby-forum.com/t/subdomains-with-rails-2-2/154758 "2008-12-03T17:19:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Perry\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/eb8c5e/32.png) [@Perry\_D](https://www.ruby-forum.com/u/Perry_D)\
**Post date:** [December 3, 2008, 5:19pm UTC](https://www.ruby-forum.com/t/subdomains-with-rails-2-2/154758/1 "2008-12-03T17:19:42Z")

</div>

I’m pretty new here, but if anybody had ideas on how to go about using  
subdomains for accounts within Rails 2.2. I had seen the subdomain-fu  
plugin, but it didn’t look like it plays very well with 2.2. Anyone  
know of other plugins that might be useful?

---

<div class="post-metadata">

**Author:** ![Perry\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/eb8c5e/32.png) [@Perry\_D](https://www.ruby-forum.com/u/Perry_D)\
**Post date:** [December 3, 2008, 6:19pm UTC](https://www.ruby-forum.com/t/subdomains-with-rails-2-2/154758/2 "2008-12-03T18:19:39Z")

</div>

The code to configure this is fairly trivial, try something like this  
in the controller(s) you need subdomain access.

before\_filter :load\_account  
def load\_account  
@account = Account.find(:first, :conditions =\> [“slug = ?”,  
request.subdomains.first])  
raise ActiveRecord::RecordNotFound unless @account  
end

---

<div class="post-metadata">

**Author:** ![Perry\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/eb8c5e/32.png) [@Perry\_D](https://www.ruby-forum.com/u/Perry_D)\
**Post date:** [December 4, 2008, 3:01pm UTC](https://www.ruby-forum.com/t/subdomains-with-rails-2-2/154758/3 "2008-12-04T15:01:39Z")

</div>

That’s awesome. I put that code into my application.rb file and it  
seems to work great!

In addition, I am looking to a couple of other subdomains not attached  
to accounts (ie- [admin.domain.com](http://admin.domain.com)). I installed the request\_routing  
plugin and it seems to work great. It gives me some good options  
within the routes.rb file that allow me to direct the root of the  
application to different controllers based on the subdomains. Like  
this:

map.root :controller =\> ‘public\_home’, :action =\> ‘index’, :conditions  
=\> { :subdomain =\> ‘’ }  
map.root :controller =\> ‘public\_home’, :action =\> ‘index’, :conditions  
=\> { :subdomain =\> ‘www’ }  
map.root :controller =\> ‘system’, :action =\> ‘index’, :conditions =\>  
{ :subdomain =\> ‘admin’ }  
map.root :controller =\> ‘account\_home’, :action =\>  
‘index’, :conditions =\> { :subdomain =\> /.+/ }

This seems to work great with Rails 2.2.

---

<div class="post-metadata">

**Author:** ![Perry\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/eb8c5e/32.png) [@Perry\_D](https://www.ruby-forum.com/u/Perry_D)\
**Post date:** [December 5, 2008, 10:44pm UTC](https://www.ruby-forum.com/t/subdomains-with-rails-2-2/154758/4 "2008-12-05T22:44:41Z")

</div>

This was the plugin I used.

[http://github.com/veilleperso/request\_routing/tree/master](http://github.com/veilleperso/request_routing/tree/master)

---

<div class="post-metadata">

**Author:** ![Perry\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/eb8c5e/32.png) [@Perry\_D](https://www.ruby-forum.com/u/Perry_D)\
**Post date:** [December 5, 2008, 6:56am UTC](https://www.ruby-forum.com/t/subdomains-with-rails-2-2/154758/5 "2008-12-05T06:56:52Z")

</div>

Hi,

Im looking to implement user based subdomains and catch-all routes  
into my app. Can subdomain-fu do this? I followed Ryan B.’  
railscast on this but he does not go into dynamically added subdomains  
and routes. Can any of you point me to some resources for getting  
started on this?  
Thanks!
