I have a model with has_one_attached :picture
.
I implemented a method which should download the picture for the object
def load_picture
RestClient.get(url, :accept => 'image/jpg')
end
(url attribute is working, i.e. tested by opening in a browser, it is downloading the picture)
Trying RestClient Gem here but any other solution would work too.
After the download I try to attach with
MyModel.picture.attach(load_picture)
...but it fails. Probaly because the attachment is nil
.
My question: How to download the image using a simple get request? Do I have to call something like .attachment
on the response?
CodePudding user response:
I tried the answer "@Christopher Oezbek" suggested:
Did the changes stated there and ir works! Perfect! Thanks!`
CodePudding user response:
Try this:
YourModel.picture.attach(
io: RestClient.get(url, :accept => 'image/jpg'),
filename: 'IMAGE_NAME.jpg',
content_type: 'image/jpg'
)
If you want more, go on https://edgeguides.rubyonrails.org/active_storage_overview.html#attaching-files-to-records. It will help you a lot.