登录后将用户定向到各自的子域

时间:2017-07-08 14:13:33

标签: ruby-on-rails ruby devise apartment-gem

我的用户可以注册该应用程序并转到他们在屏幕中输入的相应子域。我希望他们在重新登录时重定向回相同的子域。我没有收到错误的说法,但是当我在系统中重新登录时,不会在网址前面插入子域名。

application_controller.rb

skip_before_action :verify_authenticity_token
def after_sign_in_path_for(resource_or_scope)
   root_url(:subdomain => resource.subdomain) 
end 

def after_sign_out_path_for(resource_or_scope)
  root_url(subdomain: false)
end 

的routes.rb

class SubdomainPresent
  def self.matches?(request)
    subdomains = %w{ www admin }
    request.subdomain.present? && !subdomains.include?(request.subdomain)
  end
end

class SubdomainBlank
   def self.matches?(request)
    request.subdomain.blank?
   end
end

Rails.application.routes.draw do

resources :accounts
devise_for :users, controllers: { registrations: 'accounts', sessions: 'sessions' } 
get 'contact-us', to: 'contacts#new', as: 'new_contact'

constraints(SubdomainBlank) do
    root 'visitors#new'
    get 'about', to: 'pages#about'
    get 'services', to: 'pages#services'
    resources :contacts, only: :create
    resources :visitors, only: [:new, :create]
end 

constraints(SubdomainPresent) do
    root 'tasks#index', as: :subdomain_root
    resources :users do
      resource :profile
    end
    resources :tasks
    resources :deals
    resources :companies
end

end

0 个答案:

没有答案