1

I implemented the code for removing images from my User model.

Rails 5.2 Active Storage purging/deleting attachements

I think that it removed the image OK, but now I'm getting an error

undefined method `signed_id' for nil:NilClass

My view (images.html.erb):

<% if @user.images.attached? %>
  <% @user.images.each do |image| %>
    <%= image_tag(url_for(image))%>
     <%= link_to 'Remove', delete_image_attachment_user_url(image.signed_id),
            method: :delete,
            data: { confirm: 'Are you sure?' } %>
  <% end %>
<% end %>

The controller

def images
end

def delete_image_attachment
  @image = ActiveStorage::Blob.find_signed(params[:id])
  @image.purge
  redirect_to root_path
end

I tried removing code from the view, so it just has this:

<% if @user.images.attached? %>
  <% @user.images.each do |image| %>
    <%= image_tag(url_for(image))%>
  <% end %>
<% end %>

But even after I remove the code for 'signed_id', I still receive the error. Also, I'm unsure why it looks at the line receiving the code:

<%= image_tag(url_for(image))%>

if I'm using when I have the line @user.images.attached?

I thought the error might be that there were no attached images (although there should be one attached), so I added that check.

yellowreign
  • 3,528
  • 8
  • 43
  • 80
  • Hi! I'm getting the same error. Did you solve it? – Proz1g Jul 03 '18 at 21:39
  • @Proz1g No - I still don't know how to correct it. – yellowreign Jul 06 '18 at 13:04
  • Hi. I solved my problem with deleting the database and creating a new one. I know that is not a great solution, but to me at least, it works. Another option is to delete all data from the active storage. You can see how to do this here: https://stackoverflow.com/questions/51175944/remove-all-data-from-active-storage/51179019?noredirect=1#comment89369446_51179019 – Proz1g Jul 06 '18 at 13:33
  • Another thing, you might need to change something on your function. I think this method is not the correct one to delete attachments: `@image = ActiveStorage::Blob.find_signed(params[:id]`. You can check how I change it here: https://stackoverflow.com/questions/51170577/undefined-method-signed-id-for-nilnilclass?noredirect=1#comment89326338_51170577 Hope it helps you! Best regards ;) – Proz1g Jul 06 '18 at 13:35

0 Answers0