我在使用Mongoid和Devise的Rails中有一个用户类。我似乎无法弄清楚如何添加管理员角色。 platformtec(Devise)网站上的How To要求我运行标准的Rails迁移,但由于Mongoid而无法正常工作。
有人能指出我正确的方向吗?
这是我的user.rb(减去已注释掉的模块):
class User
include Mongoid::Document
# Include default devise modules.
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
## Database authenticatable
field :email, :type => String, :null => false, :default => ""
field :encrypted_password, :type => String, :null => false, :default => ""
## Recoverable
field :reset_password_token, :type => String
field :reset_password_sent_at, :type => Time
## Rememberable
field :remember_created_at, :type => Time
## Trackable
field :sign_in_count, :type => Integer, :default => 0
field :current_sign_in_at, :type => Time
field :last_sign_in_at, :type => Time
field :current_sign_in_ip, :type => String
field :last_sign_in_ip, :type => String
## Token authenticatable
# field :authentication_token, :type => String
field :name
validates_presence_of :name
validates_uniqueness_of :name, :email, :case_sensitive => false
attr_accessible :name, :email, :password, :password_confirmation, :remember_me
end
谢谢, 查理马吉
答案 0 :(得分:3)
您只需要在布尔中添加admin
字段,以便在您的课程中添加以下行:
field :admin, :type => Boolean, :default => false
这与AR中的迁移完全相同:
class AddAdminToUsers < ActiveRecord::Migration
def self.up
add_column :users, :admin, :boolean, :default => false
end
def self.down
remove_column :users, :admin
end
end
在添加之后,在设计维基中关于管理员的所有方法都可以正常工作。