如果子域名不是www',我想将请求路由到特殊控制器。
www.example.com // home#index
section1.example.com // section#index
section1.example.com/name123 // section#name
section9.example.com // section#index
routes.rb
root 'home#index'
如何强制请求使用具有上述条件的节控制器?
答案 0 :(得分:1)
#config/routes.rb
scope constraints: Section do #-> only subdomain routes (non WWW)
resources :section, path: "", only: :show #-> http://sectionx.url.com/:id
end
root "home#index" #-> should be last so that above routes are parsed first
#lib/section.rb
module Section
def initializer(router)
@router = router
end
def self.matches?(request)
Section.exists?(request.subdomain) if request.subdomain.present? && request.subdomain != "www"
end
end