Ask A Question


You’re not receiving notifications from this thread.

How do I create a delete button for images uploaded with Active Storage

Georgie Reynolds asked in Rails

I am creating a user interface for users to upload images and delete ones they've already uploaded. It is using Rails 5.2 Active Storage and Amazon S3.

The issue I am experiencing is similar to the question here but dropping the database isn't a viable solution for my project: Rails 5.2 ActiveStorage undefined method `signed_id' for nil:NilClass

I followed the answer here to create the link that users can click to delete an image. I think it is deleting the image. However, when the page reloads I get this error

Can't resolve image into URL: undefined method `signed_id' for nil:NilClass

After clicking the "Remove" link, the page should reload and show the remaining images. How do I resolve this error?

Here is the model

class Space < ApplicationRecord
  belongs_to :user

  has_many_attached :space_image


The controller

class SpacesController < ApplicationController
  before_action :set_space, except: [:index, :new, :create, :delete_image_attachment]
  before_action :authenticate_user!, except: [:show]

  def delete_image_attachment
    @space_image = ActiveStorage::Blob.find_signed(params[:id])
    redirect_back(fallback_location: spaces_path)

    def set_space
      @space = Space.find(params[:id])

The view

  <% if @space.space_image.attached? %>
    <% @space.space_image.each do |space_image| %>
    <%= image_tag space_image, class: "avatar-medium" %>
    <%= link_to 'Remove', delete_image_attachment_space_url(space_image.signed_id),
                method: :delete,
                  data: { confirm: 'Are you sure?' } %>
    <% end %>
  <% end %>

** Server error output **

ActionView::Template::Error (Can't resolve image into URL: undefined method `signed_id' for nil:NilClass):
    1: <div>
    2:   <% if @space.space_image.attached? %>
    3:     <% @space.space_image.each do |space_image| %>
    4:     <%= image_tag space_image, class: "avatar-medium" %>
    5:     <%= link_to 'Remove', delete_image_attachment_space_url(space_image.signed_id),
    6:                 method: :delete,
    7:                   data: { confirm: 'Are you sure?' } %>

app/views/spaces/_spaceimg.html.erb:4:in `block in _app_views_spaces__spaceimg_html_erb___4211978368865358240_70218939472720'
app/views/spaces/_spaceimg.html.erb:3:in `_app_views_spaces__spaceimg_html_erb___4211978368865358240_70218939472720'
app/views/spaces/listing.html.erb:121:in `block in _app_views_spaces_listing_html_erb__2591928334825339114_70218934956920'
app/views/spaces/listing.html.erb:15:in `_app_views_spaces_listing_html_erb__2591928334825339114_70218934956920'

My original Stackoverflow question


Answered on Stackoverflow. I needed to change the delete method in my controller to this:

def delete_image_attachment
    @space_image = ActiveStorage::Attachment.find(params[:id])
  redirect_back(fallback_location: request.referer)

And this line change the link in the view to this:

<%= link_to 'Remove', delete_image_attachment_space_url(,

Is it working properly?

@space_image = ActiveStorage::Blob.find_signed(params[:id])

I've found this way doesn't work correctly — the image still connected (.attached? = true)

One of the solutions can be:

Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 78,890+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more.

    © 2023 GoRails, LLC. All rights reserved.