From 978c44b682bd7bc32d16f38d1ea361bc527a9381 Mon Sep 17 00:00:00 2001 From: songtianlun Date: Fri, 3 Jan 2025 10:55:42 +0800 Subject: [PATCH] feat: add user profile editing functionality - Implemented edit and update actions in UsersController - Created edit user view and form partial - Updated user model validation to allow nil password - Modified header to link to user settings - Added integration tests for successful and unsuccessful edits This commit introduces the ability for users to edit their profile information, including name and email. It also includes validation updates to allow users to update their profiles without changing their password. Integration tests ensure that both successful and unsuccessful edit attempts are handled correctly. --- app/controllers/users_controller.rb | 15 +++++++++++++ app/models/user.rb | 2 +- app/views/layouts/_header.html.erb | 2 +- app/views/users/_form.html.erb | 18 +++++++++++++++ app/views/users/edit.html.erb | 13 +++++++++++ app/views/users/new.html.erb | 19 ++-------------- test/integration/users_edit_test.rb | 34 +++++++++++++++++++++++++++++ 7 files changed, 84 insertions(+), 19 deletions(-) create mode 100644 app/views/users/_form.html.erb create mode 100644 app/views/users/edit.html.erb create mode 100644 test/integration/users_edit_test.rb diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index bc85b72..87bf36a 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -22,6 +22,21 @@ class UsersController < ApplicationController end end + def edit + @user = User.find(params[:id]) + end + + def update + @user = User.find(params[:id]) + if @user.update(user_params) + flash[:success] = "Profile updated" + redirect_to @user + # redirect_to user_url(@user) + else + render 'edit' + end + end + private def user_params diff --git a/app/models/user.rb b/app/models/user.rb index aec527c..1f45758 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -8,7 +8,7 @@ class User < ApplicationRecord format: { with: VALID_EMAIL_REGEX }, uniqueness: true has_secure_password - validates :password, presence: true, length: { minimum: 6 } + validates :password, presence: true, length: { minimum: 6 }, allow_nil: true def User.digest(string) cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST : diff --git a/app/views/layouts/_header.html.erb b/app/views/layouts/_header.html.erb index 4f128d7..fa4e5b5 100644 --- a/app/views/layouts/_header.html.erb +++ b/app/views/layouts/_header.html.erb @@ -25,7 +25,7 @@