如果我的用户具有特定角色,则无法在 Rails 中重定向
Having trouble redirecting in Rails if my user has a certain role
我正在使用 Rails 4.2.3。我想为我的用户分配角色,所以我在 app/model/users.rb 文件中有这个…
class User < ActiveRecord::Base
has_many :assignments
has_many :roles, through: :assignments
def role?(role)
roles.any? { |r| r.name.underscore.to_sym == role }
end
def admin?
role? "Admin"
end
end
但是当我用我系统中唯一的用户登录时,我没有被重定向到管理页面(而是执行了下面的“else”子句......
def create
user = User.from_omniauth(env["omniauth.auth"])
session[:user_id] = user.id
if user.admin?
render 'admin/index'
else
redirect_to root_path
end
end
我不明白这是为什么。以下是我的 PostGres 数据库中目前唯一的数据……
myproject=> select * FROM Assignments;
id | user_id | role_id | created_at | updated_at
----+---------+---------+----------------------------+----------------------------
4 | 8 | 1 | 2016-05-10 21:15:01.863456 | 2016-05-10 21:15:01.863456
(1 row)
myproject=> select * FROM users;
id | provider | uid | name | oauth_token | oauth_expires_at | created_at | updated_at | email
----+---------------+-----------------------+---------------+-------------+------------------+----------------------------+----------------------------+-------------------------
8 | google_oauth2 | 177611649021871409999 | Dave A | | | 2016-05-10 21:15:01.861259 | 2016-05-10 21:15:01.861259 | myemail@gmail.com
(1 row)
myproject=> select * FROM roles;
id | name | created_at | updated_at
----+-------+----------------------------+----------------------------
1 | Admin | 2016-04-28 19:55:43.473016 | 2016-04-28 19:55:43.473016
2 | User | 2016-04-28 19:55:43.492222 | 2016-04-28 19:55:43.492222
您正在将字符串 "Admin" 传递给 role?
,但在传递给 roles.any?
的块中,您正在匹配一个符号。这应该有效:
def role?(role)
roles.any? { |r| r.name.underscore.to_sym == role.to_s.underscore.to_sym }
end
用这个你可以将 "Admin"
、"admin"
或 :admin
传递给 role?
我正在使用 Rails 4.2.3。我想为我的用户分配角色,所以我在 app/model/users.rb 文件中有这个…
class User < ActiveRecord::Base
has_many :assignments
has_many :roles, through: :assignments
def role?(role)
roles.any? { |r| r.name.underscore.to_sym == role }
end
def admin?
role? "Admin"
end
end
但是当我用我系统中唯一的用户登录时,我没有被重定向到管理页面(而是执行了下面的“else”子句......
def create
user = User.from_omniauth(env["omniauth.auth"])
session[:user_id] = user.id
if user.admin?
render 'admin/index'
else
redirect_to root_path
end
end
我不明白这是为什么。以下是我的 PostGres 数据库中目前唯一的数据……
myproject=> select * FROM Assignments;
id | user_id | role_id | created_at | updated_at
----+---------+---------+----------------------------+----------------------------
4 | 8 | 1 | 2016-05-10 21:15:01.863456 | 2016-05-10 21:15:01.863456
(1 row)
myproject=> select * FROM users;
id | provider | uid | name | oauth_token | oauth_expires_at | created_at | updated_at | email
----+---------------+-----------------------+---------------+-------------+------------------+----------------------------+----------------------------+-------------------------
8 | google_oauth2 | 177611649021871409999 | Dave A | | | 2016-05-10 21:15:01.861259 | 2016-05-10 21:15:01.861259 | myemail@gmail.com
(1 row)
myproject=> select * FROM roles;
id | name | created_at | updated_at
----+-------+----------------------------+----------------------------
1 | Admin | 2016-04-28 19:55:43.473016 | 2016-04-28 19:55:43.473016
2 | User | 2016-04-28 19:55:43.492222 | 2016-04-28 19:55:43.492222
您正在将字符串 "Admin" 传递给 role?
,但在传递给 roles.any?
的块中,您正在匹配一个符号。这应该有效:
def role?(role)
roles.any? { |r| r.name.underscore.to_sym == role.to_s.underscore.to_sym }
end
用这个你可以将 "Admin"
、"admin"
或 :admin
传递给 role?