Skip to content

Instantly share code, notes, and snippets.

@yoones
Last active July 25, 2019 21:21
Show Gist options
  • Save yoones/ef21423f5dba07cfa5841a5eb2e78681 to your computer and use it in GitHub Desktop.
Save yoones/ef21423f5dba07cfa5841a5eb2e78681 to your computer and use it in GitHub Desktop.
# Model
class Profile < ApplicationRecord
has_one_attached :avatar
accepts_nested_attributes_for :avatar_attachment, allow_destroy: true
end
# Controller
class ProfilesController < ApplicationController
before_action :set_profile, only: [:show, :edit, :update, :destroy]
# [...]
# PATCH/PUT /profiles/1
# PATCH/PUT /profiles/1.json
def update
respond_to do |format|
if @profile.update(profile_params)
format.html { redirect_to @profile, notice: 'Profile was successfully updated.' }
format.json { render :show, status: :ok, location: @profile }
else
format.html { render :edit }
format.json { render json: @profile.errors, status: :unprocessable_entity }
end
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_profile
@profile = Profile.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def profile_params
params.require(:profile).permit(
:last_name, :first_name,
:avatar,
avatar_attachment_attributes: [:id, :_destroy]
)
end
end
# View
<p id="notice"><%= notice %></p>
<p>
<strong>Avatar:</strong><br />
<% if @profile.avatar.attached? %>
<%= form_for @profile do |f| %>
<%= f.fields_for :avatar_attachment_attributes do |avatar_form| %>
<%= avatar_form.hidden_field :id, value: @profile.avatar_attachment.id %>
<%= avatar_form.hidden_field :_destroy, value: true %>
<% end %>
<%= f.submit "Delete avatar" %>
<% end %>
<% end %>
</p>
@aaron
Copy link

aaron commented Jul 25, 2019

I tried this out and found that it works to delete the entry from the active_storage_attachments table but leaves the active_storage_blobs entry and the associated file intact. It appears a background job would be required to check for blobs with no attachments and purge them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment