我需要使用设计为我的应用创建一个管理员角色。我使用devise创建了基本身份验证。我的应用程序中有一个设计用户模型,但现在我需要一个可以显示编辑并销毁所有用户的管理员。我试过按照教程,但没有一个帮助。 我使用rails 3.0.10和ruby 1.9.2-p290。
答案 0 :(得分:3)
您只需先创建migratioin
来定义role.rb rails g model role name:string
然后在role.rb
class Role
has_one:user
end
在用户模型中
class user
belongs_to :role
end
将两个角色插入DB
1.admin
2.user
然后检查
if user.role.name == "admin"
# can do all your logic
else
your logic
end
确保将role_id:integer插入用户模型
试试.......
答案 1 :(得分:1)
我有与您相似的要求,也不希望任何用户能够注册。所有这些都将由管理员照顾。她,我做了什么。
我添加了另一个名为Admin
的设计模型rails generate devise MODEL
禁用用户模型的“可注册”,以便用户无法自己单独使用
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!
希望这有帮助。