Search code examples
ruby-on-railsrubydeviseruby-on-rails-6

Rails 6 Devise Parameters fname, lname being passed to server, but are not being inserted int database


I'm trying to have devise insert into a user's first and last name into the database, but the values are not being inserted into the new user

Here's the server output

Started POST "/users" for 127.0.0.1 at 2020-04-16 21:14:29 -0400
Processing by Devise::RegistrationsController#create as HTML
  Parameters: {"authenticity_token"=>"5gYkDxtixNdkvQY0gFj31pLq7JEFt9vnPGFdrHg886DrjEiFVEP609sUXExl8IVwDcymqXk0zLSCT2DV1aAigg==", "user"=>{"fname"=>"John", "lname"=>"Doe", "email"=>"johndoe@gmail.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "usertype"=>"2"}, "commit"=>"Create Account"}
   (0.1ms)  begin transaction
  User Exists? (0.2ms)  SELECT 1 AS one FROM "users" WHERE "users"."email" = ? LIMIT ?  [["email", "johndoe@gmail.com"], 
["LIMIT", 1]]
  User Create (0.8ms)  INSERT INTO "users" ("email", "encrypted_password", "created_at", "updated_at") VALUES (?, ?, ?, ?)  [["email", "johndoe@gmail.edu"], ["encrypted_password", "$2a$11$nIx0QHZsX.Z.oU2wqTv4teaAG5q0TRXJCHBTEPFZrBHpKzOjq1xQ2"], ["created_at", "2020-04-17 01:14:29.854891"], ["updated_at", "2020-04-17 01:14:29.854891"]]
   (12.6ms)  commit transaction
Redirected to http://localhost:3000/
Completed 302 Found in 117ms (ActiveRecord: 13.7ms | Allocations: 6840)

I want user create to include fname and lname.

Here's my application_controller

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception
  before_action :authenticate_user!
  before_action :configure_permitted_parameters, if: :devise_controller?

  def hello; end

  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.permit(:sign_up) do |u|
      u.permit(:fname, :lname, :email, :password,
               :password_confirmation, :usertype)
    end
    devise_parameter_sanitizer.permit(:account_update) do |u|
      u.permit(:fname, :lname, :email, :password,
               :password_confirmation, :current_password)
    end
  end
end

Edit: Here is my user model too.

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

And here is my table schema

sqlite> .schema users
CREATE TABLE IF NOT EXISTS "users" ("id" integer NOT NULL PRIMARY KEY, "email" varchar DEFAULT '' NOT NULL, "encrypted_password" varchar DEFAULT '' NOT NULL, "reset_password_token" varchar DEFAULT NULL, "reset_password_sent_at" datetime DEFAULT NULL, "remember_created_at" datetime DEFAULT NULL, "created_at" datetime(6) NOT NULL, "updated_at" datetime(6) NOT NULL, "fname" varchar DEFAULT NULL, "lname" varchar DEFAULT NULL, "bdate" varchar DEFAULT NULL, "usertype" integer DEFAULT NULL);
CREATE UNIQUE INDEX "index_users_on_email" ON "users" ("email");
CREATE UNIQUE INDEX "index_users_on_reset_password_token" ON "users" ("reset_password_token");

Edit 2: I tried restructuring my configure_permitted_parameters like this as suggested but still did not work.

def configure_permitted_parameters
    devise_parameter_sanitizer.permit(:sign_up, keys: [:fname, :lname, :email, :password, :usertype])
    devise_parameter_sanitizer.permit(:account_update, keys: [:fname, :lname, :email, :password, :current_password])
  end

And the server output when creating a user at user/sign_up

Started POST "/users" for 127.0.0.1 at 2020-04-17 07:43:40 -0400
Processing by Devise::RegistrationsController#create as HTML
  Parameters: {"authenticity_token"=>"M5hZ4+SvyeUo2oMlGmmPY2nVv0txr6otpDIRzDP78MyJ7ym4bm8I+Obuh9HX4nGpva5cD3EqVO0gjOTNqGDTPQ==", "user"=>{"fname"=>"John", "lname"=>"Doe", "email"=>"johndoe@gmail.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "usertype"=>"2"}, "commit"=>"Create Account"}
   (0.0ms)  begin transaction
  User Exists? (0.3ms)  SELECT 1 AS one FROM "users" WHERE "users"."email" = ? LIMIT ?  [["email", "johndoe@gamil.com"], 
["LIMIT", 1]]
  User Create (1.2ms)  INSERT INTO "users" ("email", "encrypted_password", "created_at", "updated_at") VALUES (?, ?, ?, ?)  [["email", "johndoe@gamil.com"], ["encrypted_password", "$2a$11$I9/GPvJBCm7m8y.4V3/vC.ntkZQAA911CgQf3TvPlR7CGZibLnYYS"], ["created_at", "2020-04-17 11:43:41.145992"], ["updated_at", "2020-04-17 11:43:41.145992"]]
   (5.3ms)  commit transaction
Redirected to http://localhost:3000/
Completed 302 Found in 126ms (ActiveRecord: 6.7ms | Allocations: 6971

Edit 3: My goal is to get at sign_up when devise adds a user to the database I want

User Create (1.1ms)  INSERT INTO "users" ("email", "encrypted_password", "created_at", "updated_at") VALUES (?, ?, ?, ?)

to look like this

 User Create (1.1ms)  INSERT INTO "users" ("email", "encrypted_password", "created_at", "updated_at", "fname", "lname") VALUES (?, ?, ?, ?, ?, ?) 

Solution

  • The issue was that I had the attr_accessor :fname, :lname, :bdate, :usertype in the User model. This was preventing the information from being added to the database.