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 
<https://github.com/rails/rails/issues/33362#issuecomment-405080017> 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. 👍

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to rubyonrails-core+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-core@googlegroups.com.
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.

Reply via email to