2015-11-13 91 views
0

我想弄清楚如何重定向到不同的devise_scope用戶模型的根。能夠在每個範圍內登錄/註銷不帶任何問題,但根目錄始終只作爲第一個(公司)工作。 我試圖改變device_scope範圍仍然沒有運氣Rails 4 - Devise_scope,根不適用於多種類型的用戶(STI)

/routes.rb 

authenticated :user do 

devise_scope :company do 
root :to => 'company/main#account', :as => :company_root 
get 'main/account', :to => 'company/main#account', :as => :company_main_account 
end 

devise_scope :employee do 
root :to => 'employee/main#account', :as => :employee_root 
get 'employee/main/account', :to => 'employee/main#account', :as => :employee_main_account 
end 

devise_scope :professional do 
root :to => 'professional/main#account', :as => :professional_root 
get 'professional/main/account', :to => 'professional/main#account', :as => :professional_main_account 
end 
end 

unauthenticated do 
root :to => 'welcome#index' 
end 

嘗試過這種做法,以及沒有運氣也,根不工作的所有範圍

root :to => 'company/main#account', :constraints => lambda { |request|request.env['warden'].user.class.name == 'Company' }, :as => "company_root" 
root :to => 'employee/main#account', :constraints => lambda { |request| request.env['warden'].user.class.name == 'Employee' }, :as => "employee_root" 

感覺就像我已經嘗試了所有可能的方式,請任何幫助表示讚賞。

回答

0

你可以用一個根嘗試,​​

而在你welcome控制器,處理它

def index 
    if user_signed_in? 
     if user.class.name == "Company" 
     redirect_to company_root_path(current_user) 
     elsif user.class.name == "Employee" 
     redirect_to employee_root_path(current_user) 
     else 
     redirect_to professional_root_path(current_user) 
     end 
    end 
end 

可能有一些更好的解決方案,但我想你可以用這一個開始的。

+0

感謝您的解決方案,它按預期工作。 將用戶更新爲'current_user',導致'user'發生錯誤。我有一個問題,我的員工根的網址有23號不知道它是什麼,http://0.0.0.0:3000/employee/main%23account。你知道如何隱藏這個號碼嗎? – AlexRor

+0

這可能是登錄用戶的ID。你可能想看看'friendly_id'寶石 –

+0

我發現它是什麼。我使用員工/主要#賬戶而不是員工/主要/賬戶。現在看起來完美。感謝幫助! – AlexRor