Setup

Our current Rails app is made out of sub-apps that are mounted as engines. Typically these engines are mounted on a subdomain in the main routes.rb file as follows

mount MySubApp::Engine => '/', as: :sub_app, constraints: {subdomain: 'sub_app'}

The Problem

Routes within MySubApp 's routes.rb file do not get the subdomain when using the named _url helpers. For example the following in apps/my_sub_app/config/routes.rb

MySubApp::Engine.routes.draw do resources :foos end

gives us sub_app.foo_url(5) but it results in

http://www.example.com/foos/5

when we want

http://sub_app.example.com/foos/5

tl;dr

How can I get the engine's mounting constraints passed to its named routes?

EDIT: A Workaround

While I'd still prefer a better solution, the following will work. You can wrap all the routes in each of the sub apps routes.rb files that could be mounted on a subdomain like so

MySubApp::Engine.routes.draw do constraints Rails.application.routes.named_routes[:sub_app].constraints do resources :foos end end

EDIT 2: A much less desirable workaround