0

I am not sure how to do this. I have a table called "animals". Then i can add an image to display the animal.

I use this statement in the form-view for changing the image:

f.file_field :attachment, :accept => 'image/jpeg'

When i submit the form i got a nice looking image in my show-view.

Like i said, i can easily change the image, but i do not know exactly how to delete it. In a perfect world i would prefer, i press a link and the image gets just deleted, but the rest of the animals attributes stay alife, so the following columns are set to NULL and the file gets deleted from the server:

"attachment_file_name" "attachment_content_type" "attachment_file_size" "attachment_update_at" (should probably be set to the current date, or set to NULL)

Any help apreciated.

Donselm
  • 77
  • 1
  • 8

1 Answers1

1

If you are using paperclip gem all you need to do is set attachment to nil and save. Like this:

@animal.attachment = nil
@animal.save

More info here

In your case, if you want to add a link to remove, you will create a route and an action just to do this.

I suggest adding a checkbox nearby the file field and verify it at update/create.

rneves
  • 2,013
  • 26
  • 35
  • Thanks for your reply. Yes i am using paperclip - but i did not know that i am using it - since i took over the platform (always hard if you are not the originial programmer). Now the hint with paperclip helped a lot. Due to this i found another thread on stack overflow where the problem was discussed and solved: http://stackoverflow.com/questions/4435826/rails-paperclip-how-to-delete-attachment <- Based on this i integrated a check box into my form with which it is possible to delete the image and all files from the server. Thanks your for your reply and pointing me into the right direction! – Donselm Jul 16 '16 at 13:58