0
votes

I am using devise for user authentication on my RoR web app, I also want to use "devise_invitable" gem to make invitations to friends. I follow the documentation but got an error when I click on "Send invitation email" button. click to see devise_invatable gem

What I've done so far is:

  1. Add devise_invitable gem

  2. Add :invitable flag to the existing devise user model

devise :invitable, :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable

  1. Add DeviseInvitable to my "users" table

    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
    

    end

The error message looks like this:

PG::NotNullViolation: ERROR: null value in column "first_name" violates not-null constraint DETAIL: Failing row contains (3, null, null, null, [email protected], $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

1 Answers

0
votes

check your schema.rb table users.

You will see that it has a column first_name with the constraint null:false

This is your User that I retrieved from your log message

3, null, null, null, [email protected], $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 is the user id, while one of those null values will be the first_name

with this gem you added columns to the users table, when you send an invitation you want to update that row by inserting the following details in the table

The row is the one with id=3 so it already exists

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"

When the db tries to save the changes the error is triggered, try to open the rails console with rails c and update that record.

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

then try again with the same record