I have the below files to update a users details. they currently work for everything except for the password. all of the logic is working correctly, I have used puts to check that the correct sections of logic are entered however it seems that the password never gets updated at the end when @user.update(user_params) is called.
To get this to work I have to add the 2 lines below into the password logic in UserController which doesn't seem logical given that I call @user.update(user_params) further on which should update the user with the user parameters provided. Any advice, suggestions or comments would be greatly appreciated.
@user.password = params[:user][:password]
@user.save
I'm not using any Gems etc for the authentication and my user table looks like the following
create_table "users", force: :cascade do |t|
t.string "username"
t.string "password_digest"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.string "title"
t.string "firstName"
t.string "surname"
t.index ["username"], name: "index_users_on_username", unique: true
end
UserController
class UsersController < ApplicationController
skip_before_action :authorized, only: [:new, :create]
before_action :set_user, only: [:update]
def new
@user = User.new
end
def create
@user = User.create(params.require(:user).permit(:username, :password))
session[:user_id] = @user.id
redirect_to '/welcome'
end
def update
errorMessage = ''
if current_user.username != params[:user][:username]
if User.find_by(username: params[:user][:username]).nil?
else
errorMessage += "Username is already taken"
end
else
end
if !params[:user][:password].blank?
if @user.authenticate(params[:user][:password_confirmation])
else
errorMessage += "Confirmation password is incorrect"
end
else
end
if !errorMessage.blank?
redirect_to account_path, notice: errorMessage
else
@user.update(user_params)
redirect_to account_path
end
end
private
def user_params
params.require(:user).permit(:username, :title, :firstName, :surname,:password, :password_confirmation)
end
def set_user
@user = current_user
end
end
edit.html.erb
<p id=”notice”><%= notice %></p>
<%= form_for current_user do |f|%>
<div class="form-group row col-md-12">
<%= f.label :username, class:"col-sm-2 col-form-label"%><br>
<%= f.text_field :username, class:"form-control col-sm-10" %>
</div>
<div class="form-group row col-md-12">
<div class="col-md-2">
<%= f.label :title%><br>
<%= f.text_field :title, class:"form-control" %>
</div>
<div class="col-md-5">
<%= f.label :firstName, "First Name" %><br>
<%= f.text_field :firstName, class:"form-control" %>
</div>
<div class="col-md-5">
<%= f.label :surname %><br>
<%= f.text_field :surname, class:"form-control" %>
</div>
</div>
<div class="form-group row col-md-12">
<div class="col-md-6">
<%= f.label :password_confirmation, "Current Password"%><br>
<%= f.password_field :password_confirmation, class:"form-control" %>
</div>
<div class="col-md-6">
<%= f.label :password, "New Password"%><br>
<%= f.password_field :password, class:"form-control" %>
</div>
<small id="passwordHelpBlock" class="form-text text-muted col-md-12">
To update your password, please confirm your current password.
</small>
</div>
<%= f.submit "Submit" ,class: "btn btn-primary"%>
<% end %>
User Model
class User < ApplicationRecord
has_secure_password
end