Allow users to edit their profile

This commit is contained in:
yodalightsabr 2025-05-04 18:09:11 -07:00
parent b6861b219d
commit 10e2557c1f
7 changed files with 93 additions and 22 deletions

View file

@ -1,7 +1,7 @@
class UsersController < ApplicationController
before_action :set_user, only: %i[ show ]
before_action :require_authentication, only: [:show]
before_action :set_user, only: %i[ show edit update ]
before_action :require_authentication, only: [:show, :edit, :update]
# def index
@ -9,7 +9,6 @@ class UsersController < ApplicationController
# end
def show
@user = User.find(params[:id])
end
# def new
@ -25,25 +24,25 @@ class UsersController < ApplicationController
# end
# end
# def edit
# end
def edit
end
# def update
# if @user.update(user_params)
# redirect_to @user
# else
# render :edit, status: :unprocessable_entity
# end
# end
def update
if @user.update(user_params)
redirect_to @user, notice: "User was successfully updated."
else
render :edit, status: :unprocessable_entity
end
end
private
def set_user
@user = User.find(params[:id])
end
def set_user
@user = User.find(params[:id])
end
def user_params
params.expect(user: [ :email ])
end
def user_params
params.expect(user: [ :email, :first_name, :last_name, :github, :username ])
end
end

View file

@ -3,4 +3,8 @@ class User < ApplicationRecord
validates :email, uniqueness: true, presence: true, allow_nil: false
has_many :posts, dependent: :destroy
def name
"#{first_name} #{last_name}"
end
end

View file

@ -0,0 +1,50 @@
<%= form_with model: @user do |form|%>
<div class="">
<%= @user.email %>
<div class="mt-4">
<%= form.label :first_name %>
<div class="">
<%= form.text_field :first_name, class: "bg-[#B8DCFC] text-black border border-gray-300 p-2 rounded w-[400px]" %>
</div>
</div>
<div class="mt-4">
<%= form.label :last_name %>
<div class="">
<%= form.text_field :last_name, class: "bg-[#B8DCFC] text-black border border-gray-300 p-2 rounded w-[400px]" %>
</div>
</div>
<div class="mt-4">
<%= form.label :username %>
<div class="">
<%= form.text_field :username, class: "bg-[#B8DCFC] text-black border border-gray-300 p-2 rounded w-[400px]" %>
</div>
</div>
<div class="mt-4">
<%= form.label :github %>
<div class="">
<%= form.text_field :github, class: "bg-[#B8DCFC] text-black border border-gray-300 p-2 rounded w-[400px]" %>
</div>
</div>
<% if @user.errors.any? %>
<div class="bg-red-100 text-red-700 p-4 rounded mb-4">
<h2><%= pluralize(@user.errors.count, "thing") %> you need to fix!</h2>
<ul>
<% @user.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="mt-8">
<%= form.submit "Submit", class: "bg-[#62D5C3] text-[#3A5CE1] px-4 py-2 rounded cursor-pointer hover:opacity-100 opacity-80" %>
</div>
</div>
<% end %>

View file

@ -0,0 +1,4 @@
<h1>Edit user</h1>
<%= render "form", user: @user %>
<%= link_to "Cancel", @user %>

View file

@ -1,10 +1,12 @@
<div class="bg-[#3A5CE1] rounded-md border-2 border-[#96ABF9] p-8 mx-24 mt-24 text-center">
<h1 class="text-2xl">Fiona Hackworth</h1>
<p>@fiona.hackworth</p>
<h1 class="text-2xl"><%= @user.name %></h1>
<p>@<%= @user.username %></p>
<p class="opacity-70">
<%= pluralize(@user.posts.count, "post") %> created
<span>// <%= pluralize(9, "hour") %> spent on </span>
<p>
<% if current_user == @user %>
<%= link_to "Edit", edit_user_path(@user), class: "opacity-70 hover:opacity-100"%>
<% end %>
</div>

View file

@ -0,0 +1,8 @@
class AddDetailsToUsers < ActiveRecord::Migration[8.0]
def change
add_column :users, :first_name, :string
add_column :users, :last_name, :string
add_column :users, :username, :string
add_column :users, :github, :string
end
end

6
site/db/schema.rb generated
View file

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[8.0].define(version: 2025_05_04_201326) do
ActiveRecord::Schema[8.0].define(version: 2025_05_04_204551) do
create_table "action_text_rich_texts", force: :cascade do |t|
t.string "name", null: false
t.text "body"
@ -64,6 +64,10 @@ ActiveRecord::Schema[8.0].define(version: 2025_05_04_201326) do
t.datetime "login_code_expires_at"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "first_name"
t.string "last_name"
t.string "username"
t.string "github"
end
add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id"