Skip to content

Commit

Permalink
Merge pull request #122 from netvvrk/edit-user
Browse files Browse the repository at this point in the history
allow user editing
  • Loading branch information
bhoggard authored Dec 20, 2024
2 parents d3c8e1e + 48bb19a commit 3bbb336
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 9 deletions.
Binary file modified .yarn/install-state.gz
Binary file not shown.
11 changes: 9 additions & 2 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,14 @@ def create

# PATCH/PUT /users/1
def update
if @user.update(user_params)
name = user_params[:name]
params = user_params.except(:name)
if params[:password].empty?
params = params.except(:password, :password_confirmation)
end

if @user.update(params)
@user.profile.update_attribute(:name, name)
redirect_to users_path, notice: "User was successfully updated.", status: :see_other
else
render :edit, status: :unprocessable_entity
Expand Down Expand Up @@ -77,7 +84,7 @@ def set_user

# Only allow a list of trusted parameters through.
def user_params
params.require(:user).permit(:name, :email, :password, :password_confirmation, :role)
params.require(:user).permit(:name, :email, :password, :password_confirmation, :role, :active)
end

def confirm_admin
Expand Down
12 changes: 6 additions & 6 deletions app/views/users/_user.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<div id="<%= dom_id user %>">
<tr>
<td>
<% if user.has_profile? %>
<% if user.profile.present? %>
<%= link_to user.profile&.name, profile_path(user.profile)%>
<% end %>
</td>
Expand All @@ -18,17 +18,17 @@
login_as_user_path(user),
class: "tw-btn-secondary lg:mr-2"
) %>
<%= link_to(
"change password",
edit_user_path(user),
class: "tw-btn-secondary lg:mr-2"
) %>
<%= link_to(
"send welcome email",
send_welcome_email_user_path(user),
class: "tw-btn-secondary lg:mr-2"
) %>
<% end %>
<%= link_to(
"edit",
edit_user_path(user),
class: "tw-btn-secondary lg:mr-2"
) %>
</td>
</tr>
</div>
41 changes: 40 additions & 1 deletion app/views/users/edit.html.erb
Original file line number Diff line number Diff line change
@@ -1,2 +1,41 @@
<h1 class="font-bold pb-3">Editing <%= @user.email %></h1>
<%= render "form", user: @user %>

<%= form_with(model: @user) do |f| %>
<div class="max-w-md grid grid-cols-1 gap-y-6 ">
<% if @user.errors.any? %>
<div class="text-red-500 my-4 w-full border-red-200 py-2 border-b">
<ul>
<% @user.errors.each do |error| %>
<li>
<%= error.full_message %>
</li>
<% end %>
</ul>
</div>
<% end %>
<div>
<%= f.label :name, class: "form-label required" %>
<%= f.text_field :name, class: "small-field w-full", value: @user.profile.name%>
</div>
<div>
<%= f.label :email, class: "form-label required" %>
<%= f.email_field :email, class: "small-field w-full" %>
</div>
<div>
<%= f.label :role, class: "form-label required" %>
<%= f.select :role, ["artist", "admin"] %>
</div>
<div>
<%= f.check_box :active %> Active
</div>
<div>
<%= f.label :password, class: "form-label" %>
<%= f.text_field :password, class: "small-field w-full" %>
</div>
<div>
<%= f.label :password_confirmation, class: "form-label" %>
<%= f.text_field :password_confirmation, class: "small-field w-full" %>
</div>
<%= f.submit "Save" , class: "tw-btn-primary" %>
<% end %>
</div>

0 comments on commit 3bbb336

Please sign in to comment.