Rails 4 - Devise_scope,root 不适用于多种类型的用户 (STI)

Rails 4 - Devise_scope, root not working for multiple type of users (STI)

我正在尝试弄清楚如何为不同的 devise_scope 用户模型重定向到 root。能够在每个范围 w/o 问题中签名 in/out,但 root 始终只作为第一个(公司)工作。 我尝试将 device_scope 更改为 asscope 仍然没有运气

/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 不适用于所有范围

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"

感觉我已经尝试了所有可能的方法,请提供任何帮助。

你可以用一个根试试,root :to => 'welcome#index'

然后在您的 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

可能有一些更好的解决方案,但我想你可以从这个开始。