Skip to content

Attachments should be detached when assigned a blank value #33362

@kylefox

Description

@kylefox

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))

Steps to reproduce

  1. Assign an attachable to the attachment: @user.image = blob.signed_id
  2. Assign an empty string to the attachment: @user.image = ''

Expected behavior

  • The attachment is detached, i.e. @user.image.detach.

Actual behavior

  • ActiveSupport::MessageVerifier::InvalidSignature is raised.

System configuration

  • Rails version: Rails 5.2.0
  • Ruby version: ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-darwin17]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions