-1

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?

mu is too short
  • 426,620
  • 70
  • 833
  • 800
Julian
  • 27
  • 6
  • 1
    Hey Julian, have you checked other answers such as: https://stackoverflow.com/questions/61853410/how-to-attach-image-from-the-url-in-rails-active-storage – Christopher Oezbek Dec 20 '21 at 21:04

2 Answers2

0

I tried the answer "@Christopher Oezbek" suggested:

Did the changes stated there and ir works! Perfect! Thanks!`

Julian
  • 1
  • 2
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 21 '21 at 23:20
0

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.

led8
  • 1
  • 3