1

A while back I tried to delete attachments of the active storage so I follow the answer of this question: Rails 5.2 Active Storage purging/deleting attachements

But after doing this:

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

I start getting this error every time I entered a view with images or a link to the destroy method:

enter image description here

So I change the code a bit:

routes.rb

match 'vehicles/:id/:image_id' => 'vehicles#delete_image_attachment', :via => :delete, :as => :delete_image_attachment

vehicles_controller.rb

def delete_image_attachment
  @image = ActiveStorage::Attachment.find(params[:image_id])
  @image.purge
  redirect_back(fallback_location: vehicles_path)
end

in a view that I want to delete some image

<% @vehicle.images.each do |img| %>
  <%= image_tag img %>
  <%= link_to delete_image_attachment_url(@vehicle, img), method: :delete, data: { confirm: 'Are you sure?' } %>
<% end %>

That kind of works for the vehicles that already existed, but when I create a new vehicle the same error appears in every view with an image of that vehicle.

Does anyone knows what kind of error is this, and how can I fix it? Also, there is any way to delete all data from active storage, like resetting the table? Thank you!

Proz1g
  • 1,177
  • 4
  • 16
  • 36
  • Maybe that record doesn't own an image? Try add `if img.attached?` or something like this. Check the docs of ActiveStorage so you can see how to check whether the attachment exist. – iGian Jul 04 '18 at 10:18
  • Could you share code where `signed_id` has been used? – Anand Jul 04 '18 at 10:23
  • Hi @iGian, to be more simple to explain I've removed some code, but in my original code I check if there is some img or not with that helper method. But thank you for the answer ;) – Proz1g Jul 04 '18 at 10:35
  • @Gabbar that code was used in the vehicle controller, but I swap that `@image = ActiveStorage::Blob.find_signed(params[:id])` by `@image = ActiveStorage::Attachment.find(params[:image_id])`, like I said in the question. I think that first code destroy something in activestorage table, I dont know...Maybe there is some way to reset the tables, but I don't know how to do that. – Proz1g Jul 04 '18 at 10:39
  • Is the first vehicle 's image attached when creating a vehicle? **|||** For resetting, I never tried!, but maybe from console deleting all rows of `ActiveStorage::Attachment` and `ActiveStorage::Blob` – iGian Jul 04 '18 at 10:50
  • I end up dropping the database, I think it is some error with it, I dont know. Thank you for the help. If someone finds out the solution, please answer it ;) – Proz1g Jul 04 '18 at 15:18

0 Answers0