Rails Domain Constraints ( to serve multiple domains )
With the fine help of the great people in #RubyOnRails
on irc
I got this figured out. So thanks crankharder
and sevenseacat
for your input and advice.
What I ended up with was this:
class DomainConstraint def initialize(domain) @domains = domain end def matches?(request) @domains.include? request.host endend
and:
require 'domain_constraint'Rails.application.routes.draw do constraints DomainConstraint.new('api.project.dev') do resources :statuses root :to => 'statuses#index', as: 'api_root' end constraints DomainConstraint.new('admin.api.project.dev') do resources :statuses root :to => 'statuses#new' endend
You can also constrain a route based on any method on the
Request
object that returns aString
. http://guides.rubyonrails.org/routing.html#request-based-constraints
The methods available to Request
include host
, which could be used as follows:
constraints host: 'api.project.dev' do resources :statuses root to: 'statuses#index', as: 'api_root' end constraints host: 'admin.api.project.dev' do resources :statuses root to: 'statuses#new' end