fix: handle user activation email errors

- Wrap the activation email sending in a begin-rescue block
- Log error messages if sending fails
- Provide user feedback with an error message and render the new user form again

This change improves the user experience by handling potential
errors during the user activation email process. If an error
occurs, it logs the issue and informs the user to try again,
preventing confusion and enhancing the robustness of the
registration flow.
This commit is contained in:
songtianlun 2025-01-08 17:46:07 +08:00
parent a3f6c9770f
commit 812073b148

View File

@ -28,9 +28,15 @@ class UsersController < ApplicationController
# flash[:success] = "Welcome to the Sample App!"
# redirect_to @user
# redirect_to user_url(@user)
@user.send_activation_email
flash[:info] = "Please check your email to activate your account."
redirect_to root_url
begin
@user.send_activation_email
flash[:info] = "Please check your email to activate your account."
redirect_to root_url
rescue => e
logger.error "User creation failed: #{e.message}"
flash[:error] = "Something went wrong. Please try again."
render "new", status: :unprocessable_entity
end
else
render "new", status: :unprocessable_entity
end