Search code examples
ruby-on-railssaassubdomain

rails 3.2 link a subdomain to a controller?


Update: rewritten question a bit. Trying to route my subdomains like below

  • login.app.ltd
  • user1.app.ltd
  • user2.app.ltd
  • signup.app.ltd

Using

  • Rails 3.2
  • Devise

To no avail tried several tutorials blog posts, anyone knows a working example for this? Really stuck on this :(

this is my routes now:

 match '', to: 'frontend#index', constraints: lambda { |r| r.subdomain.present? && ( r.subdomain != 'www') }
  #match '' => 'home#index', :constraints => { :subdomain => 'login' }


  constraints :subdomain => /^(?!signup\b)(\w+)/ do
    root :to => "frontend#index"
  end

  root :to => "frontend#index"

Solution

  • My RailsApps project offers a complete example app showing how to use subdomains:

    Rails Tutorial for Subdomains with Devise

    Did you take a look at that?

    config/routes.rb

    devise_for :users
    resources :users, :only => :show
    constraints(Subdomain) do
      match '/' => 'profiles#show'
    end
    root :to => "home#index"
    

    lib/subdomain.rb

    class Subdomain
      def self.matches?(request)
        case request.subdomain
        when 'www', '', nil
          false
        else
          true
        end
      end
    end