How to add a picture to a model (User) in Rails using ActiveStorage and the Cloudinary Upload Widget?

Viewed 184

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!

0 Answers
Related