ActiveRecord::StatementInvalid in Devise::RegistrationsController#create

Question

I'm trying to add some extra information to my Devise User model like first_name, last_name, age, gender and city.

When I fill the signup form and click to submit I get this error:

SQLite3::ConstraintException: NOT NULL constraint failed: users.first_name: INSERT INTO "users" ("email", "encrypted_password", "created_at", "updated_at") VALUES (?, ?, ?, ?)

def each
  loop do
    val = step
    break self if done?
    yield val
  end

And these are the parameters if they are any use:

Parameters:

{"utf8"=>"✓", "authenticity_token"=>"2e5oUwMw84HtwSuI09X1O5kjPLYk7SW4VKgGOOxcB93W7sSQYjPgq3N/BGo0+oAEifhec4lQ3PUt9vub17vs7g==", "user"=> {"first_name"=>"Test", "last_name"=>"Test", "age"=>"69", "city"=>"New York", "gender"=>"Trans", "email"=>"test.test@email.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up"}

Here is my schema.rb just in case:

create_table "users", force: :cascade do |t|
    t.string   "email",                  default: "", null: false
    t.string   "encrypted_password",     default: "", null: false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          default: 0,  null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string   "current_sign_in_ip"
    t.string   "last_sign_in_ip"
    t.string   "confirmation_token"
    t.datetime "confirmed_at"
    t.datetime "confirmation_sent_at"
    t.string   "unconfirmed_email"
    t.string   "first_name",                          null: false
    t.string   "last_name",                           null: false
    t.integer  "age",                                 null: false
    t.string   "city",                                null: false
    t.string   "gender",                              null: false
    t.datetime "created_at",                          null: false
    t.datetime "updated_at",                          null: false
    t.index ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true
    t.index ["email"], name: "index_users_on_email", unique: true
    t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
  end

Here is my user.rb model too:

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
end

Show source
| ruby   | ruby-on-rails   | sqlite3   2017-03-13 23:03 2 Answers

Answers ( 2 )

  1. 2017-03-13 23:03

    You have a unique index on email. What the error is saying is that the email you are trying to save is not null in the database. A user already has this email. Change the email and it should work.

      # to check for the user in the database 
      rails c 
      user = User.find_by_email("test.test@email.com") should bring back a user. 
    
  2. 2017-03-13 23:03

    To have additional columns on user not already set in devise I allowed access in my ApplicationContoller this way. Rails4

    before_filter :configure_permitted_parameters, if: :devise_controller?
    
    protected
    
    def configure_permitted_parameters
      # Only add some parameters
      devise_parameter_sanitizer.for(:accept_invitation).concat [:first_name, :last_name]
    end
    

    or

    before_action :configure_permitted_parameters, if: :devise_controller?
    
    protected
    
    def configure_permitted_parameters
      devise_parameter_sanitizer.for(:sign_up) << :full_name
    end
    

    and this would allow you to have first_name last_name with the other things that you have set up.

    Rails5

    def configure_permitted_parameters
      additional_params = [:name, :company, :email_confirmation,   {addresses_attributes: [:address1, :address2, :city, :state, :zip, :country, :name]}]
      devise_parameter_sanitizer.permit(:sign_up, keys: additional_params)
      devise_parameter_sanitizer.permit(:account_update, keys: additional_params)
    end
    
◀ Go back