Pirun Seng

Ruby on Rails Developer

How to Upload Files From Remote Location With Carrierwave

Carrierwave is a classier solution for file uploads for Rails, Sinatra and other Ruby web frameworks.

I am going to demonstrate How to: Upload files from a remote location to JSONB or Array image column. For installation and configuration, I would recommend you to read Carrierwave document.

Let say we have an Avatar model with an image column of array data type. To create a new Avatar record with an image:

1
2
3
avatar = Avatar.new
avatar.remote_image_urls = ['https://image.flaticon.com/icons/png/512/25/25231.png']
avatar.save

Note: We use remote_image_urls for jsonb or array data type column while we use remote_image_url for string data type column.

Please feel tree to replace image to any column name you use to store the file or image. e.g If your Avatar model has a column named photo, then you might want to modify the syntax above to remote_photo_url and remote_photo_urls.

You can dig deeper to its source code here.

Happy coding!