onder
onder

Reputation: 837

Rails 5 Adding some attributes to `User` in Devise

I am using Rails 5.1, I added devise for user authentication. I added some extra attributes to my table. After I create a new user via ajax User created but some attribute is missing. I checked the log, devise doesn't send these columns in the insert query.

First I added the devise_parameter_sanitizer to my application controller :

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

I added attr_accessor to my User model:

attr_accessor :password1,:password2, :phone, :postalcode, :first_name, :last_name, :address

end following code my create methods in registration controller :

    @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

I am seeing following log in my console :

SQL (0.5ms) INSERT INTO "users" ("email", "encrypted_password", "created_at", "updated_at", "user_role", "latitude", "longitude", "coordinate") VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING "id" [["email", "[email protected]"], ["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"], ["latitude", 53.3960084], ["longitude", -2.929237], ["coordinate", "[53.3960084, -2.929237]"]]

And why is my code save coorditanes, longitute and lattitude attributes to db and dont save first_name, last_name and address attributes? How can I fix this? Best

EDITED : My js code :

            $.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(),
                }
            },

My User 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

end

EDITED 2 : I made test with :

 @u = User.create(first_name: params[:sign_up][:first_name], email: 
 params[:sign_up][:email], password: params[:sign_up][:password1])
 @u.save!

and de result is

Parameters: {"sign_up"=>{"first_name"=>"", "last_name"=>"Onder", "email"=>"[email protected]", "password1"=>"[FILTERED]", "password2"=>"[FILTERED]", "postalcode"=>"L16 2WA", "phone"=>"123456", "address"=>"address"}} (0.5ms) BEGIN User Exists (0.5ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = $1 LIMIT $2 [["email", "[email protected]"], ["LIMIT", 1]] SQL (1.0ms) INSERT INTO "users" ("email", "encrypted_password", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["email", "[email protected]"], ["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 Completed 200 OK in 218ms (Views: 0.3ms | ActiveRecord: 3.0ms)

Upvotes: 1

Views: 1838

Answers (1)

AntonTkachov
AntonTkachov

Reputation: 1784

Remove attr_accessor from your model

Upvotes: 2

Related Questions