Dynamic VHosts One-liner for Rails #
So, how do Tada and Basecamp do dynamic virtual hosts to give each user their own subdomain? Drop this in ApplicationController
:
before_filter { |c| c.account = Account.find_by_subdomain(c.request.subdomains.first) }
Bravo. Requires a ServerName *.tadalist.com
in the Apache VHost configuration and an accounts
table with a subdomain
field. (seen among #rubyonrails quotes.)
Update: If you’re struggling to get this to work, mpet45 has more. Configuring DNS and all that.
cilibrar
Sweet.
mrmargolis
very nice.
David
The c variable refers to the instance of the controller that is passed to the filter when its triggered. This instance holds a reference to the request object, which again has an array of all the subdomains of which we need the first (so that we get “37signals” out of 37signals.clientsection.com).
Then we have the Account model that has a subdomain field, which in the case of 37s would have “37signals”. We search for the account matching this subdomain with the dynamic finder and assign it to the instance variable “account” on the controller instance we got in. Be sure to have something like “attr_writer :account” so that variable can be assigned.
Easy as pie.
why
In BIND (and most other DNS ), you can set up a catch-all for subdomains.
textbox
very nice site!
Comments are closed for this entry.