ActiveAdmin,现有(但裸露)用户模型中的 Devise 2 不起作用
问题:如何使用 ActiveAdmin 和 Devise 2 实现下面的模型设计?
我已经使用现有的 User
模型成功设置了 active_admin 来引导它。
我假设这个模型设计意味着(“让管理员用户管理用户”)。
这是我当前的模型设置:
irb(main):003:0> User.column_names
=> ["id", "created_at", "updated_at", "avatar", "name"]
irb(main):004:0> AdminUser.column_names
=> ["id", "email", "encrypted_password", "reset_password_token", "reset_password_sent_at", "remember_created_at", "sign_in_count", "current_sign_in_at", "last_sign_in_at", "current_sign_in_ip", "last_sign_in_ip", "created_at", "updated_at"]
现在我想为我的用户模型添加一些身份验证。但是,我无法在没有此冲突的情况下迁移 railsgenerate devise User
的结果:
== AddDeviseToUsers: migrating ===============================================
-- change_table(:users)
-> 0.7201s
-- add_index(:users, :email, {:unique=>true})
rake aborted!
An error has occurred, this and all later migrations canceled:
PG::Error: ERROR: could not create unique index "index_users_on_email"
DETAIL: Key (email)=() is duplicated.
: CREATE UNIQUE INDEX "index_users_on_email" ON "users" ("email")
Tasks: TOP => db:migrate
现在我当前的 User 模型中没有 email
属性,但 AdminUser 有。因此,当 Devise 尝试在 User
中创建电子邮件属性时,我怀疑这就是我收到此错误的原因。但为什么?它们有不同的型号吗?
任何帮助,例如经验、帖子或教程,将不胜感激(以及答案)
注意我已经尝试了在 Devise wiki 和 此处,迁移没有成功。
Question: How can I achieve the model design below using ActiveAdmin and Devise 2?
I have set up active_admin successfully with an existing User
model for bootstrapping it.
I assumed this model design means ("Have adminusers manage users").
Here is my current model set up:
irb(main):003:0> User.column_names
=> ["id", "created_at", "updated_at", "avatar", "name"]
irb(main):004:0> AdminUser.column_names
=> ["id", "email", "encrypted_password", "reset_password_token", "reset_password_sent_at", "remember_created_at", "sign_in_count", "current_sign_in_at", "last_sign_in_at", "current_sign_in_ip", "last_sign_in_ip", "created_at", "updated_at"]
Now I would like to add some authenticaton for my User model. However I am unable to migrate the results of rails generate devise User
without this conflict:
== AddDeviseToUsers: migrating ===============================================
-- change_table(:users)
-> 0.7201s
-- add_index(:users, :email, {:unique=>true})
rake aborted!
An error has occurred, this and all later migrations canceled:
PG::Error: ERROR: could not create unique index "index_users_on_email"
DETAIL: Key (email)=() is duplicated.
: CREATE UNIQUE INDEX "index_users_on_email" ON "users" ("email")
Tasks: TOP => db:migrate
Now I do not have an email
attribute in my current User model, but AdminUser does. So when Devise tries to create the email attribute in User
I suspect this is why I get this error. But why? They are in different models?
Any help e.g. experiences, posts or tutorials would be appreciated (as well as an answer)
NOTE I have tried the solution found on the Devise wiki and here, with no success on migration.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

问题出在索引的创建上。由于您正在更改表,我假设您已经在其中有记录 - 尝试添加电子邮件列并随后在其上添加唯一索引将不起作用,因为该列尚不具有唯一值。 Devise 在电子邮件列上添加索引,因为它是主要登录字段 - 如果您有不同的登录字段(例如用户名),则在其上添加索引。如果您计划开始使用电子邮件进行登录,请暂时删除索引,并在使用唯一值填充电子邮件列后添加索引。
The problem is in the creation of the index. Since you are changing the table, I'm assuming you already have records in it - trying to add an email column and subsequently add a unique index on it will not work because the column won't have unique values yet. Devise adds an index on the email column because it is the primary login field - if you have a different field for login (say, username), then add the index on that instead. If you are planning to start using email for login, then remove the index for now and add it once you have populated the email column with unique values.