How to add new attachinary image to an instance instead of replacing the whole array in Rails 5

388 views Asked by At

This might be a stupid question since I can't find it already asked here but I can't find a solution no matter how I tried.

I use Rails 5 with Cloudinary and Attachinary gems.

I have a model PointOfInterest with

has_attachments :photos, maximum: 3

Everything works fine if photos are uploaded at once (with simple_form). But I want my users to be able to upload photos sequentially ; for instance 1 photo at the creation of the point_of_interest and then add 2 more later on edit.

Unfortunately the update method deletes the attachments before saving the new input.

I ended up building an array with the old attachment and new one from the update and tried to overwrite the attachments:

@point_of_interest.photos = new_photoset

but I get a Runtime error => Can't modify frozen hash.

I also just realized my new_photoset array mixes Attachinary::File (previous image) and Attachinary::File::ActiveRecord_Associations_CollectionProxy (new image) so I'm pretty sure I'm not on the right track.

I tries many different approaches and I don't find the Attachinary docs very helpful. Or maybe I should investigate on the jQuery-File-Upload side? Any help will be much appreciated.

1

There are 1 answers

1
Tom Connolly On

I would suggest you split the actions in two steps. Add to your model:

has_attachment :point_of_interest_picture

and then step one would be to have the user upload a picture, and step two would be to upload two or more pictures