Dynamicaly generate routes with a given subdomain ?

my problem seems simple but I don't manage to find a simple solution...

I would like my URLs to look like :


example :

My routes have this route :

map.building 'building/:id', :controller => 'buildings', :action =>

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


use request.subdomains to know the city param

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)


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])