【发布时间】:2016-03-24 12:28:07
【问题描述】:
我一直在使用 Rails、Active Addmin 和 cancancan。除了一件事,一切都很好。最近我为我的 admin 类型的用户和 clients 添加了单独的命名空间。
在此更改之前,我以这种方式 (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' 有人知道我如何检查用户在路线中的角色吗?有没有更好的方法来做到这一点?
【问题讨论】:
-
您可以在操作过滤器之前检查应用程序控制器中的用户角色。我之前没有访问过routes.rb中的current_user。
标签: ruby-on-rails ruby devise routes cancancan