# Dynamicaly generate routes with a given subdomain?

**URL:** <https://www.ruby-forum.com/t/dynamicaly-generate-routes-with-a-given-subdomain/124772>\
**Category:** Rails\
**Created:** [January 2, 2008, 1:24pm UTC](https://www.ruby-forum.com/t/dynamicaly-generate-routes-with-a-given-subdomain/124772 "2008-01-02T13:24:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![budu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b5e925/32.png) [@budu](https://www.ruby-forum.com/u/budu)\
**Post date:** [January 2, 2008, 1:24pm UTC](https://www.ruby-forum.com/t/dynamicaly-generate-routes-with-a-given-subdomain/124772/1 "2008-01-02T13:24:14Z")

</div>

Hi,  
my problem seems simple but I don’t manage to find a simple solution…

I would like my URLs to look like :

[http://city.server.com/building/building\_name](http://city.server.com/building/building_name)

example :  
[http://paris.server.com/building/louvre](http://paris.server.com/building/louvre)

My routes have this route :

map.building ‘building/:id’, :controller =\> ‘buildings’, :action =\>  
‘show’

Now, how can I generate the correct URL with the good subdomain with the  
two parameters : city and builing\_name?

Thanks.

---

<div class="post-metadata">

**Author:** ![budu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b5e925/32.png) [@budu](https://www.ruby-forum.com/u/budu)\
**Post date:** [January 3, 2008, 11:00am UTC](https://www.ruby-forum.com/t/dynamicaly-generate-routes-with-a-given-subdomain/124772/2 "2008-01-03T11:00:09Z")

</div>

use request.subdomains to know the city param

On Jan 2, 5:24 pm, Nicolas B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![budu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b5e925/32.png) [@budu](https://www.ruby-forum.com/u/budu)\
**Post date:** [January 3, 2008, 11:13am UTC](https://www.ruby-forum.com/t/dynamicaly-generate-routes-with-a-given-subdomain/124772/3 "2008-01-03T11:13:36Z")

</div>

For your own peace of mind, use the account\_location plugin and then  
have a before\_filter in your application controller:

before\_filter :find\_city

def find\_city  
@city = City.find\_by\_name(account\_subdomain)  
end

Then you can just use the associations you defined in your City model  
to get to the building:

@city.buildings.find(:first, :conditions =\> [“buildings.name = ?”,  
params[:building\_name])

On 03 Jan 2008, at 10:59, blj wrote:

> > [http://city.server.com/building/building\_name](http://city.server.com/building/building_name)  
> > two parameters : city and builing\_name?  
> > Best regards

Peter De Berdt
