Rails 6 Devise参数fname,lname被传递到服务器,但没有被插入到数据库中



我正试图让design将用户的名字和姓氏插入数据库,但这些值没有插入到新用户的中

这是服务器输出

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)

我希望用户创建包括fname和lname。

这是我的应用程序_控制器

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

编辑:这也是我的用户模型。

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

这是我的表格模式

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");

编辑2:我试着按照建议重新构建我的configure_permitted_parameters,但仍然没有成功。

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

以及在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

编辑3:我的目标是在design将用户添加到我想要的数据库时获得sign_up

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

看起来像这个

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

问题是我在User模型中有attr_accessor :fname, :lname, :bdate, :usertype。这导致无法将信息添加到数据库中。

最新更新