5

world!

After implementing Cloudinary's Active Storage Integration, it's easy to make it work in a form.

We just need to add to our simple_form_for a

  <%= f.input :photo, as: :file %>  

BUT, if we want to use the Cloudinary Upload Widget, there's no straightforward way of adding this file to the form.

What I have tried is: 1- let the widget upload the picture; 2- update the value of a hidden input with the url of the uploaded picture; 3- in the controller, when creating the model, download the picture and attach it to the model:

  require 'open-uri'
  file = URI.open(photo_url)
  @user.photo.attach(io: file, filename: 'profile_photo.jpg') 

  # [...]

  private

  def photo_url
    params.require(:user).permit(:photo_url)[:photo_url]
  end

It works, BUT it uploads twice the same picture and - IMHO - is extremely inefficient.

Any ideas?

Thank you!

Good Luck, Have Fun!

  • 2
    I can see no way to directly push from the upload widget into ActiveStorage. ActiveStorage is very limiting and there's no good way to specify where the file is for ActiveStorage to record. I recently stripped out ActivesStorage from a project for this reason and the scalability issues it causes. In short, I don't think what you're looking for can be done. Your only shot is https://edgeguides.rubyonrails.org/active_storage_overview.html#integrating-with-libraries-or-frameworks which may allow you to push the JS event from the widget into a direct AS upload – TomDunning Oct 08 '20 at 14:15

0 Answers0