Ruby on Rails + devise:如何使用 devis rake db:migrate 创建自定义用户表?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以执行迁移以向用户表添加一些字段。
例如:
然后,为了向表中添加列,请运行:
Devise 已经生成了您需要的一些列,例如:电子邮件、密码、created_at、updated_at...
要将角色添加到您的用户模型,您应该观看 cancan 截屏视频:< a href="http://railscasts.com/episodes/192-authorization-with-cancan" rel="nofollow">railscasts 并阅读doc 以查看一些更新。
编辑:
如果您想手动添加字段,您可以在运行迁移之前将它们添加到 self.up 方法中:
You can perform a migration to add some fields to the user table.
For example:
Then, in order to add columns to your table run:
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: