title: Rails Domain Constraints ( to serve multiple domains )
body: <blockquote>
    <p>$ rails -v Rails 4.2.1</p>
    
    <p>$ ruby -v ruby 2.2.2p95 (2015-04-13 revision > 50295) [x86_64-linux]</p>
  </blockquote>
  
  <p>I am building an API for a mobile app, which will have an admin interface to it.  What i'm attempting to do, is run this through nginx using unicorn ( which I have running on my dev environment)</p>
  
  <p>I have 2 domains routed to the exact same rails project.  These domains are: <code>api.project.dev</code> and <code>admin.api.project.dev</code></p>
  
  <p>I've read this:
  <a href="http://guides.rubyonrails.org/routing.html#advanced-constraints" rel="nofollow noreferrer">http://guides.rubyonrails.org/routing.html#advanced-constraints</a></p>
  
  <p>and tried:
  <a href="https://stackoverflow.com/questions/24122140/separate-domain-for-namespaced-routes-in-rails-4">Separate Domain for Namespaced Routes in Rails 4</a> ( see answer )</p>
  
  <p>I've tried a few other things to try and get this to work, the only thing that comes up ( for either sub-domain ) is:</p>
  
  <p><code>Invalid route name, already in use: 'root'</code></p>
  
  <p>My current implementation of this is:</p>
  
  <pre><code>class DomainConstraint
    def initialize(domain)
      @domains = domain
    end
  
    def matches?(request)
      @domains.include? request.domain
    end
  end
  </code></pre>
  
  <p>and</p>
  
  <pre><code>require 'domain_constraint'
  Rails.application.routes.draw do
    resources :statuses
    constraints (DomainConstraint.new('api.project.dev')) do
      root :to =&gt; 'statuses#index'
    end
  
    constraints(DomainConstraint.new('admin.api.project.dev')) do
      root :to =&gt; 'statuses#new'
    end
  end
  </code></pre>
  
  <p>keep in mind that the roots are different pages only for now, but ultimately will be completely different systems.</p>
  
  <p>Not quite sure where to go from here in order to get this functioning as I would hope.</p>