diff --git a/app/assets/stylesheets/main.css b/app/assets/stylesheets/main.css index a585fe2..b8902aa 100644 --- a/app/assets/stylesheets/main.css +++ b/app/assets/stylesheets/main.css @@ -5,6 +5,7 @@ --dark-background-color: #3A3A3A; --inset-background-color: #FFFFFF; --alert-border-color: #800000; + --form-error-color: #FF0000; } * { @@ -119,6 +120,13 @@ form { grid-column: 1/3; border-radius: .2em .2em 0 0; } + + > label.field_with_errors { + color: var(--form-error-color); + } + > input.field_with_errors { + border-color: var(--form-error-color); + } } div.flash { diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 70ff4f1..ba41850 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,6 +1,8 @@ # frozen_string_literal: true class UsersController < ApplicationController + before_action :set_user, only: [:edit, :update, :show] + def new @user = User.new end @@ -16,12 +18,29 @@ class UsersController < ApplicationController end end - def show + def edit + head :forbidden and return unless @user == helpers.current_user + end + + def update @user = User.find_by(username: params[:id]) + if @user.update(user_params) + redirect_to @user, notice: t(".account_updated") + else + flash.now.alert = t(".update_failed") + render :edit + end + end + + def show end private + def set_user + @user = User.find_by(username: params[:id]) + end + def user_params params.require(:user).permit( :username, diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 7008c6c..bd975fd 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -27,11 +27,7 @@
- <% flash.each do |type, message| %> -
- <%= message %> -
- <% end %> + <%= render "shared/flash" %> <%= yield %>
diff --git a/app/views/shared/_flash.html.erb b/app/views/shared/_flash.html.erb new file mode 100644 index 0000000..5c363b1 --- /dev/null +++ b/app/views/shared/_flash.html.erb @@ -0,0 +1,7 @@ +
+ <% flash.each do |type, message| %> +
+ <%= message %> +
+ <% end %> +
diff --git a/app/views/users/_form.html.erb b/app/views/users/_form.html.erb new file mode 100644 index 0000000..5c7f96a --- /dev/null +++ b/app/views/users/_form.html.erb @@ -0,0 +1,23 @@ + <%= form_with model: user, id: "user_form" do |f| %> +

<%= title %>

+ + <%= f.label :username %> + <%= f.text_field :username %> + + <%= f.label :email %> + <%= f.text_field :email %> + + <%= f.label :first_name %> + <%= f.text_field :first_name %> + + <%= f.label :last_names %> + <%= f.text_field :last_names %> + + <%= f.label :password %> + <%= f.password_field :password %> + + <%= f.label :password_confirmation %> + <%= f.password_field :password_confirmation %> + + <%= f.submit button_text %> + <% end %> diff --git a/app/views/users/edit.html.erb b/app/views/users/edit.html.erb new file mode 100644 index 0000000..d421315 --- /dev/null +++ b/app/views/users/edit.html.erb @@ -0,0 +1,6 @@ +<%= render partial: "form", + locals: { + user: @user, + button_text: t(".edit"), + title: t(".edit"), + } %> diff --git a/app/views/users/edit.turbo_stream.erb b/app/views/users/edit.turbo_stream.erb new file mode 100644 index 0000000..6fdbeba --- /dev/null +++ b/app/views/users/edit.turbo_stream.erb @@ -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(".edit"), + title: t(".edit"), + } %> +<% end %> diff --git a/app/views/users/new.html.erb b/app/views/users/new.html.erb index a4ea91c..69c90a7 100644 --- a/app/views/users/new.html.erb +++ b/app/views/users/new.html.erb @@ -1,22 +1,6 @@ -<%= form_with model: @user do |f| %> -

<%= t(".register") %>

- <%= f.label :username %> - <%= f.text_field :username %> - - <%= f.label :email %> - <%= f.text_field :email %> - - <%= f.label :first_name %> - <%= f.text_field :first_name %> - - <%= f.label :last_names %> - <%= f.text_field :last_names %> - - <%= f.label :password %> - <%= f.password_field :password %> - - <%= f.label :password_confirmation %> - <%= f.password_field :password_confirmation %> - - <%= f.submit t(".register") %> -<% end %> +<%= render partial: "form", + locals: { + user: @user, + button_text: t(".register"), + title: t(".register"), + } %> diff --git a/app/views/users/show.html.erb b/app/views/users/show.html.erb index 6f6a57e..1714de3 100644 --- a/app/views/users/show.html.erb +++ b/app/views/users/show.html.erb @@ -1,3 +1,6 @@

<%= @user.username %>

<%= @user.full_name %>

+<% if @user == current_user %> + <%= link_to t(".edit_user_details"), edit_user_path(@user) %> +<% end %> diff --git a/config/application.rb b/config/application.rb index 5f62e2a..dd2fe97 100644 --- a/config/application.rb +++ b/config/application.rb @@ -12,5 +12,9 @@ module SummonPlayer config.load_defaults 7.0 config.time_zone = "London" + + config.action_view.field_error_proc = proc do |html_tag, _instance| + html_tag.gsub(/<(\w*) /, '<\1 class="field_with_errors"').html_safe + end end end diff --git a/config/locales/controllers/users/en.yml b/config/locales/controllers/users/en.yml index 413e9f8..afa9a0f 100644 --- a/config/locales/controllers/users/en.yml +++ b/config/locales/controllers/users/en.yml @@ -1,3 +1,5 @@ en: users: account_created: Account created! + account_updated: Your details have been updated. + update_failed: Could not update your details. diff --git a/config/locales/views/users/en.yml b/config/locales/views/users/en.yml index d22d34a..5950f27 100644 --- a/config/locales/views/users/en.yml +++ b/config/locales/views/users/en.yml @@ -3,3 +3,7 @@ en: new: register: Register welcome: Welcome + edit: + edit: Edit + show: + edit_user_details: Edit your account details diff --git a/config/routes.rb b/config/routes.rb index be54046..37e1d9f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -3,7 +3,7 @@ Rails.application.routes.draw do root "sessions#new" - resources :users, only: [:new, :create, :show] + resources :users, only: [:new, :create, :show, :edit, :update] resources :sessions, only: [:new, :create] delete "log_out", to: "sessions#destroy_session" end