The resolving pattern that is used to look up views can only contain variables that are registered with the ActionView::LookupContext
class. The first step is therefore to register a new variable (subdomain
) with the LookupContext
class. You should do this in an initializer:
ActionView::LookupContext.register_detail(:subdomain) do
['default_subdomain']
end
Now the LookupContext
knows about the subdomain
, it can be included in the resolving pattern. For more detail about changing the resolving pattern, see the ActionView::FileSystemResolver
documentation, but essentially you should include the following, also in an initializer:
ActionController::Base.view_paths = ActionView::FileSystemResolver.new(
Rails.root.join('app', 'views'),
':prefix/:action{.:locale,}{.:subdomain,}{.:formats,}{.:handlers,}'
)
This pattern is eventually passed to Dir.glob
(after the :*
variables have been replaced). The glob pattern {.:subdomain,}
means “either .:subdomain
or nothing”, which provides the fallback to a view file with no subdomain if the file with a subdomain isn't found.
The final step is to update your ApplicationController
to pass the subdomain to the LookupContext
:
class ApplicationController < ActionController::Base
def details_for_lookup
{:subdomain => [request.subdomain]}
end
end
(This answer was mostly figured out by reading source code, some of these features aren't documented. It was tested with Rails 3.2.5)