我有一个从头开始的身份验证系统,当用户单击“编辑配置文件”时,无论他想编辑哪个字段,都必须输入当前密码。
def update
if params[:user][:password].present?
authenticated = @user.authenticate(params[:user][:current_password])
if authenticated && @user.update(user_params)
redirect_to root_url
flash[:notice] = "Your profile was successfully updated!"
else
@user.errors.add(:current_password, 'is invalid') unless authenticated
render :edit
end
elsif @user.update(user_params)
redirect_to root_url
flash[:notice] = "Your profile was successfully updated!"
else
render :edit
end
end
当用户想要更改他的密码时,我如何才能调用身份验证或使用某些上下文模型验证?