【问题标题】:Gem devise set multiple user model systemGem 设计集多用户模型系统
【发布时间】:2012-12-17 21:05:04
【问题描述】:

在我的应用程序中,我需要做两种方式登录系统: 1) 用户 - 仅适用于网站的用户部分,包含有关用户的信息、登录数据等... 2) Admin - 网站管理部分的另一种模型。

但是如何做到这一点呢? 现在我只有第一部分和我的迁移:

class DeviseCreateUsers < ActiveRecord::Migration
  def self.up
    create_table(:users) do |t|
     ## Database authenticatable
     t.string :email, :null => false, :default => ""
     t.string :encrypted_password, :null => false, :default => ""

     ## Recoverable
     t.string :reset_password_token
     t.datetime :reset_password_sent_at

     ## Rememberable
     t.datetime :remember_created_at

     ## Trackable
     t.integer :sign_in_count, :default => 0
     t.datetime :current_sign_in_at
     t.datetime :last_sign_in_at
     t.string :current_sign_in_ip
     t.string :last_sign_in_ip

     ## Encryptable
     # t.string :password_salt

     ## Confirmable
     #t.string :confirmation_token
     #t.datetime :confirmed_at
     #t.datetime :confirmation_sent_at
     #t.string :unconfirmed_email # Only if using reconfirmable

     ## Lockable
     #t.integer :failed_attempts, :default => 0 # Only if lock strategy is :failed_attempts
     #t.string :unlock_token # Only if unlock strategy is :email or :both
     #t.datetime :locked_at

     ## Token authenticatable
     t.string :authentication_token

     t.timestamps
    end

    add_index :users, :email, :unique => true
    add_index :users, :reset_password_token, :unique => true
    #add_index :users, :confirmation_token, :unique => true
    #add_index :users, :unlock_token, :unique => true
    add_index :users, :authentication_token, :unique => true
   end

  def self.down
    drop_table :users
  end
end

还有路线:

devise_for :users

但是如何添加第二个管理员登录部分?

【问题讨论】:

  • 聪明人可以阅读docs。如果你想寻求帮助,也不要那么粗鲁。

标签: ruby-on-rails devise admin


【解决方案1】:

您有 3 个选项:

  1. 将字符串字段type 添加到您的表并从User 模型派生(管理员和用户将具有相同的登录路径)。
  2. 添加布尔字段 admin 并使用即 CanCan gem。
  3. 运行 rails g devise admin 并拥有单独的管理员模型。

您选择的方式取决于您的应用结构。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-29
    • 1970-01-01
    • 2021-09-20
    • 1970-01-01
    • 1970-01-01
    • 2012-10-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多