assembler / attachinary

Attachments handler for Rails that uses Cloudinary for storage.
MIT License
294 stars 129 forks source link

Problem implementing has_attachments #137

Open tamlyn10 opened 8 years ago

tamlyn10 commented 8 years ago

Hi there, in my Rails 5 app, I've managed to get attachinary working well with the singular (has_attachment), however, the plural/multiple files doesn't seem to be saving to my database / working in with ActiveRecord. They ARE being uploaded to my Cloudinary account but my database has no record of them. I'm applying this to a Campaign model. Any @campaign instance I create happily returns @campaign.image, @campaign.avatar. However, @campaign.photos, returns:

pry(#<CampaignsController>)> campaign.photos**
  Attachinary::File Load (0.4ms)  SELECT "attachinary_files".* FROM "attachinary_files" WHERE "attachinary_files"."attachinariable_id" = $1 AND "attachinary_files"."attachinariable_type" = $2 AND "attachinary_files"."scope" = $3  [["attachinariable_id", 50], ["attachinariable_type", "Campaign"], ["scope", "photos"]]
=> []

despite the fact that they ARE in my params (and uploaded to Cloudinary).

pry(#<CampaignsController>)> params["campaign"]["photos"]
=> ["[{\"public_id\":\"qaggxtud1tuszez416fs\",\"version\":1474468089,\"signature\":\"3cb944ed9812d8de88c16f7438a4872a35ba0e6c\",\"width\":3264,\"height\":2448,\"format\":\"jpg\",\"resource_type\":\"image\",\"created_at\":\"2016-09-21T14:28:09Z\",\"tags\":[\"development_env\",\"attachinary_tmp\"],\"bytes\":2201871,\"type\":\"upload\",\"etag\":\"4f2fca2fe80296abd8dc17ba1a8d9129\",\"url\":\"http://res.cloudinary.com/barefootbidders/image/upload/v1474468089/qaggxtud1tuszez416fs.jpg\",\"secure_url\":\"https://res.cloudinary.com/barefootbidders/image/upload/v1474468089/qaggxtud1tuszez416fs.jpg\",\"original_filename\":\"IMG_3488\"},{\"public_id\":\"sylthqxwil4y6xvjh5mn\",\"version\":1474468112,\"signature\":\"f954c2c95a6ae06c3a87327974fc4e0f02eb49ef\",\"width\":2448,\"height\":3264,\"format\":\"jpg\",\"resource_type\":\"image\",\"created_at\":\"2016-09-21T14:28:32Z\",\"tags\":[\"development_env\",\"attachinary_tmp\"],\"bytes\":2632824,\"type\":\"upload\",\"etag\":\"5448d8c5d9fab83328cbcab079656c46\",\"url\":\"http://res.cloudinary.com/barefootbidders/image/upload/v1474468112/sylthqxwil4y6xvjh5mn.jpg\",\"secure_url\":\"https://res.cloudinary.com/barefootbidders/image/upload/v1474468112/sylthqxwil4y6xvjh5mn.jpg\",\"original_filename\":\"IMG_3490\"},{\"public_id\":\"hgfl2ykpnwnu7akiel47\",\"version\":1474468131,\"signature\":\"835c9c126d5f77bcee9e3f56b0f74834e4c2c8d7\",\"width\":2448,\"height\":3264,\"format\":\"jpg\",\"resource_type\":\"image\",\"created_at\":\"2016-09-21T14:28:51Z\",\"tags\":[\"development_env\",\"attachinary_tmp\"],\"bytes\":2260244,\"type\":\"upload\",\"etag\":\"028fd4d3169c9e1a8f0d2b984614104d\",\"url\":\"http://res.cloudinary.com/barefootbidders/image/upload/v1474468131/hgfl2ykpnwnu7akiel47.jpg\",\"secure_url\":\"https://res.cloudinary.com/barefootbidders/image/upload/v1474468131/hgfl2ykpnwnu7akiel47.jpg\",\"original_filename\":\"IMG_3491\"}]"]

Archive.zip

PabloScolpino commented 8 years ago

Hello, The problem lies in the controller, the photos parameter is not properly referenced as the array it is. You have params.require(:campaign).permit(:question, :option_1, :option_2, :user_id, :image, :photos, :avatar).merge(user) Whereas you should have params.require(:campaign).permit(:question, :option_1, :option_2, :user_id, :image, photos: [], :avatar).merge(user)

I hope this helps. Regards. P.