Created
June 12, 2017 20:11
-
-
Save darrenterhune/2fa57687cdf10b769bd6e39fa466a82f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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