Allow deletion of avatars
This commit is contained in:
parent
d1175870c2
commit
1ca30a9db2
|
@ -32,7 +32,9 @@ class UsersController < ApplicationController
|
|||
end
|
||||
|
||||
def update
|
||||
if @user.update(existing_user_params)
|
||||
@user.avatar.purge if params["remove_avatar"] == "true"
|
||||
|
||||
if existing_user_params.present? && @user.update(existing_user_params)
|
||||
redirect_to @user, notice: t(".success")
|
||||
else
|
||||
flash.now[:alert] = t(".error")
|
||||
|
|
|
@ -35,6 +35,9 @@
|
|||
<hr>
|
||||
|
||||
<% if user.persisted? %>
|
||||
<%= label_tag :remove_avatar %>
|
||||
<%= check_box_tag :remove_avatar, value: true %>
|
||||
|
||||
<%= f.label :avatar %>
|
||||
<%= f.file_field :avatar %>
|
||||
<%= display_form_errors(user, :avatar) %>
|
||||
|
|
|
@ -52,6 +52,15 @@ class UsersControllerTest < ActionDispatch::IntegrationTest
|
|||
assert_redirected_to user_path(users(:trevor))
|
||||
end
|
||||
|
||||
test "can delete avatar" do
|
||||
user = users(:trevor)
|
||||
assert user.avatar.attached?
|
||||
|
||||
sign_in users(:trevor)
|
||||
patch(user_url(user), params: { remove_avatar: "true" })
|
||||
assert_not user.reload.avatar.attached?
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def user_params
|
||||
|
|
|
@ -0,0 +1,4 @@
|
|||
trevor_avatar:
|
||||
name: avatar
|
||||
record: trevor (User)
|
||||
blob: trevor_avatar_blob
|
|
@ -0,0 +1 @@
|
|||
trevor_avatar_blob: <%= ActiveStorage::FixtureSet.blob(filename: "trevor.png", service_name: "test") %>
|
Binary file not shown.
After Width: | Height: | Size: 329 KiB |
Loading…
Reference in New Issue