我在 Rails 中有一个使用 Mongoid 和 Devise 的用户类。我似乎无法弄清楚如何添加管理员角色。 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
谢谢, 查理马吉
最佳答案
你只需要在 Boolean 中添加一个 admin
字段,所以在你的类 User 中添加这一行:
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
添加后,devise wiki中所有关于admin的方法都可以正常使用。
关于ruby-on-rails - 如何在 Rails/Mongoid 用户类中添加管理员到 Devise?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9898585/