We've moved discussions to Discord

Adding own subdomains in routes.rb

JP Whitaker
Is there something special I need to do for subdomain routing?

The product I'm working on has two special landing pages, one for military and one for civilian: 

I've tried following the rails routing docs as follows:
  get '/', to: 'static#mil', constraints: {subdomain: 'mil'}

In the console I don't even see the subdomain in the request:
22:19:42 web.1     | Started GET "/" for ::1 at 2020-05-07 22:19:42 -1000
Tabish Iqbal
Hey  JP Whitaker  

I was able to change things around using subdomains internally to use something like this

class MarketingSite
  def self.matches?(request)
    request.subdomain.blank? || request.subdomain == 'www'
  end
end

class App
  def self.matches?(request)
    request.subdomain.present? && request.subdomain == 'app'
  end
end

Add that to the top of the routes file then the remaning stuff i have like this

Rails.application.routes.draw do
  # Jumpstart views
  if Rails.env.development? || Rails.env.test?
    mount Jumpstart::Engine, at: "/jumpstart"
    mount LetterOpenerWeb::Engine, at: "/letter_opener"
  end

  constraints(MarketingSite) do
    root to: "static#index"
    draw :static
    devise_scope :user do
      root to: 'users/registrations#new', as: 'new_user_registation'
    end
  end

  unauthenticated :user do
    constraints(App) do
      devise_scope :user do
        root to: 'users/sessions#new', as: ''
      end
    end
  end

  constraints(App) do
    # Administrate
    authenticated :user, lambda { |u| u.admin? } do
      namespace :admin do
        if defined?(Sidekiq)
          require "sidekiq/web"
          mount Sidekiq::Web => "/sidekiq"
        end

        resources :announcements
        resources :users
        namespace :user do
          resources :connected_accounts
        end
        resources :accounts
        resources :account_users
        resources :plans
        namespace :pay do
          resources :charges
          resources :subscriptions
        end

        root to: "dashboard#show"
      end
    end

    devise_for :user,
      controllers: {
        masquerades: "jumpstart/masquerades",
        omniauth_callbacks: "users/omniauth_callbacks",
        registrations: "users/registrations"
      }

    resources :announcements, only: [:index]
    resources :api_tokens
    resources :accounts do
      member do
        patch :switch
      end

      resources :account_users, path: :members
      resources :account_invitations, path: :invitations, module: :accounts
    end
    resources :account_invitations

    # Payments
    resource :card
    resource :subscription do
      patch :info
      patch :resume
    end
    resources :charges
    namespace :account do
      resource :password
    end

    namespace :users do
      resources :mentions, only: [:index]
    end
    namespace :user, module: :users do
      resources :connected_accounts
    end

    resources :embeds, only: [:create], constraints: {id: /[^\/]+/} do
      collection do
        get :patterns
      end
    end

    root 'dashboard#show', as: :subdomain_root, only: [:show]
  end


  match "/404", via: :all, to: "errors#not_found"
  match "/500", via: :all, to: "errors#internal_server_error"

 end

Doing it this way I have it to where I have my main site (marketing) that will just be on domain.com but when they want to sign in or sign up it will go to app.domain.com
JP Whitaker
Thanks, I'll take a closer look at this and see if it's something I can implement in my own codebase.

I was wondering if maybe  Chris Oliver  knows, perhaps there is something in the multi-tenancy code that is messing with normal operation of subdomains?
JP Whitaker
Ok I figured it out, had to do with being on localhost.

If you run into the same problem, add this to your config/environemnts/development.rb:
config.action_dispatch.tld_length = 0

Without it, my domain looked like 'mil.localhost'  where mil is the domain name and .localhost is the TLD.
Tabish Iqbal
you can also get to subdomains like:
mil.lvh.me:3000
Notifications
You’re not receiving notifications from this thread.