From 725dd6a2a5ea2fecd2c7a3f6ae6e3ab262f460e6 Mon Sep 17 00:00:00 2001 From: Lucas David Erkana Date: Tue, 11 Jul 2023 15:54:49 +0200 Subject: [PATCH 1/4] Allow user to add a photo rails generate migration AddPhotoToUsers photo:string rails db:migrate update user_controllers.rb -update user.rb -Create registrations_controller.rb -update resistration/new.html.erb -Then update routes -Apply stuling to photo display --- app/assets/stylesheets/mobile_nav.css | 10 ++++++++++ app/controllers/registrations_controller.rb | 9 +++++++++ app/controllers/users_controller.rb | 10 +++++++++- app/models/user.rb | 2 +- app/views/devise/registrations/new.html.erb | 3 +++ app/views/layouts/application.html.erb | 18 ++++++++++++------ config/routes.rb | 2 +- .../20230711111807_add_photo_to_users.rb | 5 +++++ db/schema.rb | 3 ++- 9 files changed, 52 insertions(+), 10 deletions(-) create mode 100644 app/controllers/registrations_controller.rb create mode 100644 db/migrate/20230711111807_add_photo_to_users.rb diff --git a/app/assets/stylesheets/mobile_nav.css b/app/assets/stylesheets/mobile_nav.css index c489e7e..fa497eb 100644 --- a/app/assets/stylesheets/mobile_nav.css +++ b/app/assets/stylesheets/mobile_nav.css @@ -67,6 +67,16 @@ padding: 20px; } +.avatar-img { + width: 200px; /* Adjust the width to your desired size */ + height: 210px; /* Adjust the height to your desired size */ +} + +.rounded-circle { + border-radius: 50%; /* Make the image circular by setting border-radius to 50% */ + overflow: hidden; /* Hide any content that exceeds the circular boundaries */ +} + .profile-info-container p { margin-top: 5px; text-align: center; diff --git a/app/controllers/registrations_controller.rb b/app/controllers/registrations_controller.rb new file mode 100644 index 0000000..ab78ab7 --- /dev/null +++ b/app/controllers/registrations_controller.rb @@ -0,0 +1,9 @@ +# app/controllers/registrations_controller.rb +class RegistrationsController < Devise::RegistrationsController + private + + def sign_up_params + params.require(:user).permit(:name, :email, :password, :password_confirmation, :photo) + end + end + \ No newline at end of file diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 779f395..050ed65 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -19,7 +19,12 @@ def edit; end # POST /users or /users.json def create + @user = User.new(user_params) + @user.photo = params[:user][:photo] + puts "Photo URL: #{params[:user][:photo]}" + puts "User Photo: #{@user.photo}" + puts "User: #{@user}" respond_to do |format| if @user.save @@ -31,6 +36,9 @@ def create end end end + + + # PATCH/PUT /users/1 or /users/1.json def update @@ -64,6 +72,6 @@ def set_user # Only allow a list of trusted parameters through. def user_params - params.require(:user).permit(:name) + params.require(:user).permit(:name, :email, :password, :password_confirmation, :photo) end end diff --git a/app/models/user.rb b/app/models/user.rb index da4bea6..c680471 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -6,6 +6,6 @@ class User < ApplicationRecord has_many :categories, foreign_key: 'author_id' has_many :entities, foreign_key: 'author_id' - + attribute :photo, :string validates :name, presence: true, length: { maximum: 50 } end diff --git a/app/views/devise/registrations/new.html.erb b/app/views/devise/registrations/new.html.erb index f42e671..c0d8a5e 100644 --- a/app/views/devise/registrations/new.html.erb +++ b/app/views/devise/registrations/new.html.erb @@ -21,6 +21,9 @@ <%= f.label :name %> <%= f.text_field :name, autofocus: true, placeholder: "Full name", required: true %> + <%= f.label :photo %> + <%= f.text_field :photo, placeholder: "Photo URL", required: false %> + <%= f.label :email %> <%= f.email_field :email, autocomplete: "email", placeholder: "Email", required: true %> diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 2f74cf4..6048c02 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -16,12 +16,18 @@
-
-
- -
-

<%= current_user.name.split(' ').first %>

-
+
+
+ <% if user_signed_in? && current_user.photo.present? %> + <%= image_tag current_user.photo, class: "avatar-img rounded-circle" %> + <% else %> + + <% end %> +
+

<%= current_user.name.split(' ').first %>

+
+ +