How to map a complete domain to a subdomain-based Rails application account?

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

+5
source share
2 answers

, . , . 2 Rails, , www.domain.com domain.com . , "" .

, , :

return @current_club if defined?(@current_club)

@current_club, , .

+2

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:

config.middleware.use AccountDetector
+3

All Articles