我试图弄清楚在用户登录到 Ruby/Rails3 应用程序后,如何根据他们的角色在特定 URL 上重定向用户。
到目前为止,我使用 authlogic gem 进行身份验证,使用 cancan gem 进行角色设置。
角色就像这样(在 app/models/user.rb 中定义):
class User < ActiveRecord::Base
acts_as_authentic
ROLES = %w[admin customer demo]
end
现在有 app/controllers/user_session_controller.rb 负责登录。我想做这样的事情:
for r in User.role
if r == "admin"
redirect_to admins_url
else
redirect_to users_url
end
end
由于以下错误,这不起作用:
"undefined method `role' for #<Class:0xb5bb6e88>"
有没有一种简单或优雅的方法可以根据用户的角色将用户重定向到特定的 URL?
(角色在用户表的 mysql 列“角色”中定义。)