Skip to content

Instantly share code, notes, and snippets.

@darrenterhune
Created June 12, 2017 20:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save darrenterhune/2fa57687cdf10b769bd6e39fa466a82f to your computer and use it in GitHub Desktop.
Save darrenterhune/2fa57687cdf10b769bd6e39fa466a82f to your computer and use it in GitHub Desktop.
# routes.rb (checks if subdomains is present in the request object then sends it off to the correct location)
Rails.application.routes.draw do
constraints MicrositeConstraint do
root to: 'microsites#index', as: 'microsite_root'
end
end
class MicrositeConstraint
def self.matches?(request)
taken = %w(
api
assets
images
www
)
request.subdomain.present? && !request.subdomain.in?(taken)
end
end
# application_controller.rb (used in application to load the correct model based on the subdomain)
def current_microsite
return unless request.subdomain.present? && request.subdomain != 'www'
@microsite ||= Microsite.active.find_by!(host: request.subdomain)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment