Skip to content

Instantly share code, notes, and snippets.

@yshmarov
Last active March 6, 2021 14:34
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 yshmarov/85a9a3e4572cac1b5cf682d42fb5449d to your computer and use it in GitHub Desktop.
Save yshmarov/85a9a3e4572cac1b5cf682d42fb5449d to your computer and use it in GitHub Desktop.
subdomains
constraints SubdomainRequired do
root to: 'dashboards#show', as: 'subdomain_root'
end
class SubdomainRequired
def self.matches?(request)
request.subdomain.present? && request.subdomain != "www"
end
end
ApplicationController
before_action :redirect_to_subdomain # 1.
private
def redirect_to_subdomain
return unless user_signed_in? # 2.
if request.subdomain.blank? # 3.
if current_user.accounts.any?
redirect_to root_url(subdomain: current_user.accounts.first.subdomain)
end
else # 4.
unless current_user.accounts.pluck(:subdomain).include?(request.subdomain)
redirect_to root_url(subdomain: current_user.accounts.first.subdomain)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment