我正在使用Rails 5.1,我添加了用于用户身份验证的设计。我在表格中添加了一些额外的属性。我通过ajax创建一个新用户后创建了一些属性,但缺少某些属性。我检查了日志,设计不会在插入查询中发送这些列。
首先,我将devise_parameter_sanitizer添加到我的应用程序控制器:
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:email,:password1,:password2,:title,:phone, :first_name , :last_name, :postalcode, :address,:coordinate, :latitude, :longitude])
end
我将attr_accessor添加到我的用户模型中:
attr_accessor:password1,:password2,:phone,:postalcode,:first_name, :last_name,:地址
在代码中结束我的注册控制器中的创建方法:
@user = User.new
@user.email = params[:sign_up][:email]
@user.password = params[:sign_up][:password1]
@user.password_confirmation =params[:sign_up][:password2]
@user.phone = params[:sign_up][:phone]
@firstname = params[:sign_up][:first_name]
@user.first_name = @firstname
@user.last_name = params[:sign_up][:last_name]
@user.postalcode = params[:sign_up][:postalcode]
@coordinate = Geocoder.coordinates(params[:sign_up][:postalcode])
@user.coordinate = @coordinate
@user.latitude = @coordinate[0]
@user.longitude = @coordinate[1]
@user.address = params[:sign_up][:address]
@user.valid?
if @user.errors.blank?
@user.save
@end
我在控制台中看到以下日志:
SQL(0.5ms)INSERT INTO“users”(“email”,“encrypted_password”, “created_at”,“updated_at”,“user_role”,“纬度”,“经度”, “坐标”)价值(1美元,2美元,3美元,4美元,5美元,6美元,7美元,8美元)返回“id” [[“email”,“jd @ gmail.com”],[“encrypted_password”, “$ 2A $ $ 11 Rsn8pM0wVfiRAHwMQ / 8YTeYGDn6qzAvZEFPJjqq4Ri.xZfGdof9f6”], [“created_at”,“2017-11-13 12:07:27.781262”],[“updated_at”, “2017-11-13 12:07:27.781262”],[“user_role”,“t”],[“纬度”, 53.3960084],[“经度”,-2.929237],[“坐标”,“[53.3960084,-2.929237]”]
为什么我的代码将coorditanes,longitute和lattitude属性保存到db并且不保存first_name,last_name和地址属性?我怎样才能解决这个问题? 最好
已编辑: 我的js代码:
$.ajax({
type: "POST",
url: "/users",
data: {
sign_up: {
first_name: $('#firstname').val(),
last_name: $('#lastname').val(),
email: $('#email').val(),
password1: $('#password1').val(),
password2: $('#password2').val(),
postalcode: $('#postalcode').val(),
phone: $('#phone').val(),
website: $('#website').val(),
title: $('#title').val(),
address: $('#address').val(),
}
},
我的用户Shcema
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.inet "current_sign_in_ip"
t.inet "last_sign_in_ip"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.boolean "superadmin_role"
t.boolean "user_role"
t.string "first_name"
t.string "last_name"
t.string "phone"
t.string "post_code"
t.float "latitude"
t.float "longitude"
t.string "coordinate"
t.string "address"
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
端
编辑2: 我做了测试:
@u = User.create(first_name: params[:sign_up][:first_name], email:
params[:sign_up][:email], password: params[:sign_up][:password1])
@u.save!
结果是
参数:{“sign_up”=> {“first_name”=>“”,“last_name”=>“Onder”, “email”=>“Test1603@test.com”,“password1”=>“[已过滤]”, “password2”=>“[过滤]”,“邮政编码”=>“L16 2WA”,“手机”=>“123456”, “address”=>“address”}}(0.5ms)BEGIN用户存在(0.5ms)SELECT 1 AS FROM FROM“users”WHERE“users”。“email”= $ 1 LIMIT $ 2 [[“email”, “test1603@test.com”],[“LIMIT”,1]] SQL(1.0ms)INSERT INTO“users” (“email”,“encrypted_password”,“created_at”,“updated_at”)VALUES ($ 1,$ 2,$ 3,$ 4)返回“id”[[“email”,“test1603@test.com”], [ “encrypted_password”, “$ 2A $ $ 11 K2fQmO4GcnwH3egXLDzJr.KGERWoMR93tF9Vp7OMNxqVE7KPVKfwm”], [“created_at”,“2017-11-13 13:03:56.876695”],[“updated_at”, “2017-11-13 13:03:56.876695”]](0.5ms)COMMIT(0.0ms)BEGIN
(0.5ms)COMMIT在218ms完成200 OK(浏览次数:0.3ms | ActiveRecord:3.0ms)
答案 0 :(得分:2)
从模型中删除attr_accessor