Ruby on Rails + devise:如何使用 devis rake db:migrate 创建自定义用户表?

发布于 2024-12-08 16:00:46 字数 2064 浏览 0 评论 0原文

Rails生成设备用户我得到了这个=>

class DeviseCreateUsers < ActiveRecord::Migration
  def self.up
    create_table(:users) do |t|
      t.database_authenticatable :null => false
      t.recoverable
      t.rememberable
      t.trackable

      # t.encryptable
      # t.confirmable
      # t.lockable :lock_strategy => :failed_attempts, :unlock_strategy => :both
      # t.token_authenticatable


      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

但我想创建一个用户表,其中包含用户名、电子邮件、密码、角色、组、标记、created_at、modified_at 列。

我该怎么做?

此结构是否正确,包含用户名、密码、电子邮件、组、角色、标记?

class DeviseCreateUsers < ActiveRecord::Migration
  def self.up
    create_table(:users) do |t|
      t.database_authenticatable :null => false
      t.recoverable
      t.rememberable
      t.trackable

      # t.encryptable
      # t.confirmable
      # t.lockable :lock_strategy => :failed_attempts, :unlock_strategy => :both
      # t.token_authenticatable

      t.string :username
      t.string :password
      t.string :email
      t.string :group
      t.string :role
      t.integer :mark

      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

这些是什么?

t.database_authenticatable :null => false
          t.recoverable
          t.rememberable
          t.trackable

rails generate devise User I got this=>

class DeviseCreateUsers < ActiveRecord::Migration
  def self.up
    create_table(:users) do |t|
      t.database_authenticatable :null => false
      t.recoverable
      t.rememberable
      t.trackable

      # t.encryptable
      # t.confirmable
      # t.lockable :lock_strategy => :failed_attempts, :unlock_strategy => :both
      # t.token_authenticatable


      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

But i want to create a users table with username, email, password, role, group, mark, created_at, modified_at columns.

How can i do this ?

Is this structure correct to have username, password, email, group, role, mark ?

class DeviseCreateUsers < ActiveRecord::Migration
  def self.up
    create_table(:users) do |t|
      t.database_authenticatable :null => false
      t.recoverable
      t.rememberable
      t.trackable

      # t.encryptable
      # t.confirmable
      # t.lockable :lock_strategy => :failed_attempts, :unlock_strategy => :both
      # t.token_authenticatable

      t.string :username
      t.string :password
      t.string :email
      t.string :group
      t.string :role
      t.integer :mark

      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

What are these?

t.database_authenticatable :null => false
          t.recoverable
          t.rememberable
          t.trackable

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

幽蝶幻影 2024-12-15 16:00:47

您可以执行迁移以向用户表添加一些字段。
例如:

rails g add_fields_to_users username:string # as well as other fields you need

然后,为了向表中添加列,请运行:

rake db:migrate

Devise 已经生成了您需要的一些列,例如:电子邮件、密码、created_at、updated_at...

要将角色添加到您的用户模型,您应该观看 cancan 截屏视频:< a href="http://railscasts.com/episodes/192-authorization-with-cancan" rel="nofollow">railscasts 并阅读doc 以查看一些更新。

编辑:

如果您想手动添加字段,您可以在运行迁移之前将它们添加到 self.up 方法中:

def self.up
  create_table(:users) do |t|

    #...

    t.rememberable
    t.trackable

    t.string :username
    #... your other attributes here

  end

You can perform a migration to add some fields to the user table.
For example:

rails g add_fields_to_users username:string # as well as other fields you need

Then, in order to add columns to your table run:

rake db:migrate

Devise has already generated some columns you need like: email, password, created_at, updated_at...

For adding roles to your user model you should watch the cancan screencast: railscasts and also read the doc in order to see some updates.

EDIT:

If you want to add fields manually you could add them in your self.up method before running your migration:

def self.up
  create_table(:users) do |t|

    #...

    t.rememberable
    t.trackable

    t.string :username
    #... your other attributes here

  end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文