Rails:使用可以看到所有用户的devise添加管理员角色
问题描述:
我需要使用devise为我的应用创建管理员角色。我已经使用设计创建了基本认证。我在我的应用程序中有一个设计用户模型,但现在我需要一个能够显示编辑并销毁所有用户的管理员。我试着按照教程,但没有一个帮助。 我使用rails 3.0.10和ruby 1.9.2-p290。Rails:使用可以看到所有用户的devise添加管理员角色
答
你只需通过创建migratioin
rails g model role name:string
然后在role.rb
class Role
has_one:user
end
,并在用户模式
class user
belongs_to :role
end
插入两个角色到DB
首先定义role.rb1.admin
2.user
然后通过这个
if user.role.name == "admin"
# can do all your logic
else
your logic
end
检查确保插入ROLE_ID:整数到用户模型
试试吧.......
答
我也有类似的要求,因为你的,也不要”不希望任何用户能够注册。所有这些都将由Admin管理。她所做的一切。
我添加了名为管理员
rails generate devise MODEL
禁用 '可注册' 用户模式,使用户无法通过自理singup
user.rb
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :registerable, :timeoutable and :omniauthable
devise :database_authenticatable, :recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me, :first_name, :last_name, :role, :admin
# attr_accessible :title, :body
end
启用用户CRUD另一个色器件模型通过使用来自这里的样本:https://gist.github.com/1056194
最后保护保护用户控制像这样
users_controller.rb
# Add this
before_filter :authenticate_admin!
希望这有助于。
+0
谢谢,我想我得到的解决方案将发布它! – Aayush 2012-04-20 06:06:50
优秀的解决方案。哈哈,只是注意到这是一个榜样:P – Zaz 2013-08-17 16:33:25
哦,等等,角色应该是'has_many:users',不应该吗? – Zaz 2013-08-17 16:44:02
它根据您的需要。 – Kashiftufail 2013-08-17 16:57:22