sub-domain handling?


I am not sure if this is sub-domain handling, but am interested on how this can be handled in rails.

How can an app handle the traffic to, say - The other normal traffic I guess will be handled by the main controller (of Any pointers to any tutorial/books appreciated.


The basic steps are:

1. Use the account_location plugin.

2. Make "www" a reserved subdomain.

3. Put these filters in ApplicationController:

  # We need this redirection not only for users, I founded that some bots
  # request public pages to directly. We need to respond
  # with the appropiate redirection so they don't index the error page.
  # They would because the error page is a successful HTTP response.
  def ensure_subdomain
    if account_subdomain.blank?
      redirect_to_url "http://www.#{account_domain}#{request.request_uri}"

  def find_account
    @current_account = Account.find_by_short_name(account_subdomain)
    if !@current_account"there's no account with short name
#{account_subdomain}, redirecting to the home")
      redirect_to_url "http://www.#{account_domain}"

4. Be very careful, scope everything by @current_account if necessary.
For example modify User.authenticate to receive an account:

  def self.authenticate(account, email, password)
    u = account.users.find_by_email(email) # user needed to get the salt
    u && u.authenticated?(password) ? u : nil

and in general DO NOT use find over classes as in

   Project.find(...) # WARNING, APPLICATION-WIDE

but instead

    @current_account.projects.find(...) # GOOD

That's a guideline and exceptions depend on the application, for example


may be already fine because you've validated that @current_user
belongs to @current_account somehow etc.