ActiveStorage allows attachments to be attached by assigning a string representing a blob's signed_id. This works _brilliantly_ because it enables the same params conventions used when assigning other attributes through forms → controllers → models:
params = { user: { image: 'eyJfcmF--e31aef3' } }
However, it's surprising that setting the attachment to an empty string does _not_ detach the attachment:
params = { user: { image: '' } } # => InvalidSignature
It feels more Railsy to treat the empty string as a special case that detaches the attachment.
This would allow attachments to be removed in a way that mirrors how they are added (i.e. posting hidden field values) instead of having to call @user.image.detach in a controller. For example, this would handle both attaching and detaching @user.image:
@user.update_attributes(params.require(:user).permit(:name, :email, :image))
Ideally in this scenario, if params[:user][:image] was '' ActiveStorage would call @user.image.detach.
Currently if you assign an empty string to an attachment an ActiveSupport::MessageVerifier::InvalidSignature error is raised.
Apparently this is already how
has_many_attached behaves — it'd be nice if
has_one_attached behaved similarly.
Any interest in this functionality? If so I'd be happy to put together a patch. 👍