ruby-on-rails - 如何在 Rails/Mongoid 用户类中添加管理员到 Devise?

标签 ruby-on-rails devise mongoid

我在 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/

相关文章:

ruby-on-rails - Strong Params、Nested Attributes 和 Mongoid 似乎因为 *_attributes 形式的后缀而不起作用?

ruby-on-rails - 将 MongoDB 中的 _id 类型更改为整数是否不好?

ruby-on-rails - Rails i18n中的混合语言环境

sql - 如何使 postgresql 搜索方法不区分大小写?

ruby-on-rails - 生成唯一的用户名(omniauth + devise)

ruby-on-rails - 创建自定义设计策略

rubygems - 如何在 Rails 应用程序中查看新遗迹中的 mongoid 查询

ruby-on-rails - PG::错误:错误:关系 "users"不存在

ruby-on-rails - 为什么我可以将随机代码添加到我的 Ruby 类定义中?

ruby-on-rails - rails 4 : Assigning Roles via UI Using Rolify Gem