将devise_invitable gem添加到现有设计中

时间:2017-11-07 21:58:05

标签: ruby-on-rails devise

我在我的RoR网络应用程序上使用设计用户身份验证,我还想使用“devise_invitable”gem来向朋友发送邀请。我按照文档但在单击“发送邀请电子邮件”按钮时出错。 click to see devise_invatable gem

到目前为止我所做的是:

  1. 添加devise_invitable gem

  2. 向现有设计用户模型添加:invitable标志

  3. 设计:invitable,:database_authenticatable,:registerable,      :可恢复的,:可记住的,:可跟踪的,:有效的

    1. 将DeviseInvitable添加到我的“用户”表

      def change
      add_column :users, :invitation_token, :string
      add_column :users, :invitation_created_at, :datetime
      add_column :users, :invitation_sent_at, :datetime
      add_column :users, :invitation_accepted_at, :datetime
      add_column :users, :invitation_limit, :integer
      add_column :users, :invited_by_id, :integer
      add_column :users, :invited_by_type, :string
      add_index :users, :invitation_token, :unique => true
      
      change_column_null :users, :encrypted_password, :string, true
      

    2. 错误消息如下所示:

      PG::NotNullViolation: ERROR: null value in column "first_name" violates not-null constraint DETAIL: Failing row contains (3, null, null, null, little@sheep.com, $2a$11$BcpZUt1rFlj85gdhj3F.ReTDOFNIy1FaV8cyco0gITa2TyQu7/oJy, null, null, null, 0, null, null, null, null, 2017-11-07 21:37:44.031886, 2017-11-07 21:37:44.031886, null, 4d51bd86a5cf613f64aaf0b1e0920f87441f04f7b62f4b3abcbee12ae061e806, 2017-11-07 21:37:44.030662, 2017-11-07 21:37:44.030662, null, null, 1, User). : INSERT INTO "users" ("email", "encrypted_password", "created_at", "updated_at", "invitation_token", "invitation_created_at", "invitation_sent_at", "invited_by_id", "invited_by_type") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) RETURNING "id"
      

1 个答案:

答案 0 :(得分:0)

检查您的schema.rbusers

您会看到它有一个first_name列,其约束为null:false

这是我从您的日志消息中检索到的用户

3, null, null, null, little@sheep.com, $2a$11$BcpZUt1rFlj85gdhj3F.ReTDOFNIy1FaV8cyco0gITa2TyQu7/oJy, null, null, null, 0, null, null, null, null, 2017-11-07 21:37:44.031886, 2017-11-07 21:37:44.031886, null, 4d51bd86a5cf613f64aaf0b1e0920f87441f04f7b62f4b3abcbee12ae061e806, 2017-11-07 21:37:44.030662, 2017-11-07 21:37:44.030662, null, null, 1, User

3是用户id,而其中一个null值将是first_name

使用此gem,您在users表中添加了列,当您发送邀请时,您希望按inserting更新该行,表格中包含以下详细信息

rowid=3,因此它已存在

INSERT INTO "users" ("email", "encrypted_password", "created_at", "updated_at", "invitation_token", "invitation_created_at", "invitation_sent_at", "invited_by_id", "invited_by_type") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) RETURNING "id"

当db尝试保存更改时会触发错误,请尝试使用rails c打开rails控制台并更新该记录。

user = User.find(3)
user.first_name = 'random name'
user.save

然后再次尝试使用相同的记录