我一直在使用 Rails、Active Addmin 和 cancancan。除了一件事之外,一切都工作正常。最近,我为我的管理员类型用户和客户端添加了单独的命名空间。
在此更改之前,我以这种方式将所有经过身份验证的用户重定向到同一个事件管理仪表板 (routes.rb):
devise_scope :user do
authenticated :user do
root :to => 'admin/dashboard#index', as: :authenticated_root
end
unauthenticated :user do
root :to => 'pages#index', as: :unauthenticated_root
end
end
目前,我需要以某种方式添加额外的条件,以检查经过身份验证的用户是否具有角色 admin 或 client。
我的想法是做这样的东西:
devise_scope :user do
authenticated :user do
if current_user.role?(:Architect) || current_user.role?(:Admin)
root :to => 'admin/dashboard#index', as: :authenticated_root
else
root :to => 'clients/dashboard#index', as: :authenticated_client
end
end
unauthenticated :user do
root :to => 'pages#index', as: :unauthenticated_root
end
end
但我收到错误:未定义的局部变量或方法“current_user” 有谁知道如何检查用户在 route 的角色?有没有更好的方法来做到这一点?
最佳答案
根目录是一个负责路由定义的配置文件,您无法访问配置文件中的任何 session 变量。
如果您想在登录后重定向用户,可以使用 Devise::SessionsController
中的 after_sign_in_path_for
方法
class SessionsController < Devise::SessionsController
def after_sign_in_path_for(resource)
if resource.role?(:Architect) || resource.role?(:Admin)
authenticated_root_url
else
authenticated_client_url
end
end
end
在您的 route ,您需要指示自定义session_controller
devise_for :user, :controllers => {:sessions => "sessions"}
关于ruby-on-rails - Rails、主动管理、设计、路线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36200220/