Ruby on Rails
RoutingSubdomains

Here is a quickfix for people looking for including subdomains/domain name in Routing (routes.rb)

Change line 472 in:


gems/action_pack-[VER]/lib/action_controller/routing.rb

FROM:

string_path = request.path

TO:

string_path = "/" + request.subdomains.join('/') + "/" + request.domain + request.path  

and that is it :-)

in your routes.rb you can use:


map.connect ':subdomain1/:subdomain2/[etc...]/:domain/:controller/:action/:id'

BUT

url_for gets broken – it links to:


yourdomain.com/:subdomain1/:subdomain2/[etc...]/:domain/:controller/:action/:id

Any volunteers to fix it? Look into Url for domain

Anyone knows how to “plugin” or “gem” it?

If you don’t want to change the routing.rb file directly, you can add this to the bottom of environment.rb (works fine for me with Rails 1.1), or put it in lib/somefile.rb and require “somefile.rb” from environment.rb:

class ActionController::Routing::RouteSet
	def recognize(request)
		#string_path = request.path  
		string_path = "/" + request.subdomains.join('/') + "/" + request.domain + request.path 
		string_path.chomp! if string_path[0] == ?/  
		path = string_path.split '/'  
		path.shift  
		
		hash = recognize_path(path)  
		return recognition_failed(request) unless hash && hash['controller']  
		
		controller = hash['controller']  
		hash['controller'] = controller.controller_path  
		request.path_parameters = hash  
		controller.new 
	end
    alias :recognize! :recognize
end

— Henrik N

You could also try my plugin: Request Routing.

— Dan Webb