views:

42

answers:

3

There are a lot of articles in the net about dealing with subdomain accounts in Rails. See this for example. However, I cannot find a simple way of making an account key as the top-level directory of the url. That is, I want to convert my

http://account.domain.tld url to

http://domain.tld/account

So far, the best all-around gem I've come so far to dealing with this is routing filter.

Any other ideas?

+2  A: 

I think you probably want to look at Subdomain_fu.

Mike Williamson
A: 

You need to set up a custom route with a wildcard component. Add something like this to config/routes.rb:

map.account ':/account', :controller => 'accounts', :action => 'show'

Then in AccountsController:

def AccountsController < ApplicationController
  def show

    # Probably move this into a before_filter
    @account = Account.find_by_name(params[:account])
  end
end
John Topley
A: 

I'd do this in the controllers, or perhaps even on the servers vhosts (Apache or Nginx or whatever). Here's an example that uses controllers.

# App controller
class ApplicationController < ActionController::Base
  before_filter :monitor_subdomains

  private

  def monitor_subdomains
    return if !request.subdomains.empty? && !request.subdomains.first != "www"

    account = request.subdomains.first
    redirect_to "#{account}/#{request.request_uri}"
  end
end

# routes.rb
map.account ":account_name" do |account|
  account.resources :projects
  # more account related routes...
end

I'm not sure if my routes.rb example will work, but you get the picture.

August Lilleaas