简体   繁体   English

ActiveAdmin,Devise 2在现有(但裸)用户模型不起作用

[英]ActiveAdmin, Devise 2 in existing (but bare) User model not working

Question: How can I achieve the model design below using ActiveAdmin and Devise 2? 问题:如何使用ActiveAdmin和Devise 2实现下面的模型设计?

I have set up active_admin successfully with an existing User model for bootstrapping it. 我已成功使用现有User模型设置active_admin以进行引导。

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. 现在我想为我的User模型添加一些authenticaton。 However I am unable to migrate the results of rails generate devise User without this conflict: 但是我无法迁移rails generate 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

Now I do not have an email attribute in my current User model, but AdminUser does. 现在我的当前用户模型中没有email属性,但是AdminUser有。 So when Devise tries to create the email attribute in User I suspect this is why I get this error. 因此,当Devise尝试在User创建电子邮件属性时,我怀疑这就是我收到此错误的原因。 But why? 但为什么? They are in different models? 他们有不同的模特?

Any help eg 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. 注意我已经尝试了在Devise wiki上找到的解决方案, 这里没有成功迁移。

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. Devise在电子邮件列上添加索引,因为它是主要登录字段 - 如果您有不同的登录字段(例如,用户名),则在其上添加索引。 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. 如果您计划开始使用电子邮件进行登录,请立即删除索引,并在使用唯一值填充电子邮件列后添加该索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM