2015-11-13 88 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