Fix user edit/registration

- Don't require password to edit
- Fix errors on registration form
This commit is contained in:
Trevor Vallender 2023-08-18 19:31:00 +01:00
parent ad4fa94bfa
commit c4715b172d
4 changed files with 29 additions and 1 deletions

View File

@ -14,6 +14,7 @@ class UsersController < ApplicationController
session[:user_id] = @user.id session[:user_id] = @user.id
redirect_to root_path, notice: t(".account_created") redirect_to root_path, notice: t(".account_created")
else else
flash.now.alert = t(".create_failed")
render :new render :new
end end
end end

View File

@ -16,7 +16,15 @@ class User < ApplicationRecord
validates :email, validates :email,
format: { with: /\A.*@.*\..*\z/ } # Only very basic regex format: { with: /\A.*@.*\..*\z/ } # Only very basic regex
validates :password, confirmation: true validates :password,
confirmation: true,
length: { in: 12..300 },
presence: true,
if: :validate_password?
validates :password_confirmation,
presence: true,
if: :validate_password?
def to_param def to_param
username username
@ -25,4 +33,10 @@ class User < ApplicationRecord
def full_name def full_name
"#{first_name} #{last_names}" "#{first_name} #{last_names}"
end end
private
def validate_password?
new_record? || password_digest_changed?
end
end end

View File

@ -0,0 +1,12 @@
<%= turbo_stream.replace "flash_wrapper" do %>
<%= render "shared/flash" %>
<% end %>
<%= turbo_stream.replace "user_form" do %>
<%= render partial: "form",
locals: {
user: @user,
button_text: t(".register"),
title: t(".register"),
} %>
<% end %>

View File

@ -1,5 +1,6 @@
en: en:
users: users:
create_failed: Could not create user account, please correct the errors below.
account_created: Account created! account_created: Account created!
account_updated: Your details have been updated. account_updated: Your details have been updated.
update_failed: Could not update your details. update_failed: Could not update your details.