I am developing a Rails application that by default configures user accounts to the selected subdomain. As an option, they will be able to map their full domain to their account.
So far, this is how I created things. I use subdomain-fu to enable routing:
# routes.rb map.with_options :conditions => {:subdomain => true} do |app| app.resources # User application routes are all mapped here end map.with_options :conditions => {:subdomain => false} do |www| www.resources # Public-facing sales website routes are mapped here end
In addition to this, I use the method
, . , . 2 Rails, , www.domain.com domain.com . , "" .
www.domain.com
domain.com
, , :
return @current_club if defined?(@current_club)
@current_club, , .
@current_club
Rack, , Rails.
class AccountDetector def initialize(app) @app = app end def call(env) account = Club.find_by_mapped_domain(env["HTTP_HOST"]) if account env["HTTP_HOST"] = "#{account.subdomain}.yourdomain.com" end @app.call(env) end end
environment.rb:
environment.rb
config.middleware.use AccountDetector