Rails:使用可以查看所有用户的设计添加管理员角色 [英] Rails : Adding an admin role using devise who can see all the users
问题描述
我需要使用 devise 为我的应用创建一个管理员角色.我已经使用 devise 创建了基本身份验证.我的应用程序中有一个设计用户模型,但现在我需要一个可以显示编辑和销毁所有用户的管理员.我尝试按照教程进行操作,但没有一个有帮助.我正在使用 rails 3.0.10 和 ruby 1.9.2-p290.
I need to create an admin role using devise for my app. I've created basic authentication using devise . I have a devise user model in my app but now i need an admin who can show edit and destroy all the users. I tried following the tutorials but none of them helped. I am using rails 3.0.10 and ruby 1.9.2-p290.
推荐答案
你只需先通过创建迁移定义 role.rb
You just define role.rb first by creating migratioin
rails g model role name:string
然后在role.rb
class Role
has_one:user
end
在用户模型中
class user
belongs_to :role
end
在数据库中插入两个角色
Insert two roles into DB
1.admin
2.user
然后通过这个检查
if user.role.name == "admin"
# can do all your logic
else
your logic
end
确保在用户模型中插入 role_id:integer
Make sure insert role_id:integer into user model
试试看.......
这篇关于Rails:使用可以查看所有用户的设计添加管理员角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!