RoR Carrierwave: Add remote files with multiple upload

Viewed 699

I'm using the possibility to upload multiple files at once with carrierwave as described here: https://github.com/carrierwaveuploader/carrierwave#multiple-file-uploads

So I have

class Item < ActiveRecord::Base
  mount_uploaders :images, ImagesUploader
  serialize :images, JSON
  ...
end

Now I want to upload a remote file (not from local drive). Usually I would use something like this in my controller

class ItemsController < ApplicationController
  ...
  item.remote_images_url = params[:image_url]
  ...
end

But the helper remote_images_url (mind the plural version remote_images_url) only give me

undefined method remote_images_url

I also tried remote_image_url which would be the default helper in case of single file upload. Also undefined method.

How can I upload remote files when using "multiple files upload" with carrierwave?

2 Answers

Eventually I had a look into the github repository of carrierwave and I found this:

https://github.com/carrierwaveuploader/carrierwave/blob/master/lib/carrierwave/mount.rb

Luckily it's all described in the comments in this file. There was only one little thing I had to change. In the comments it says that for a column called images I had to use the helper method remote_image_urls. But actually I have to use remote_images_urls (always plural).

So that's the solution in my case:

class Item < ActiveRecord::Base
  mount_uploaders :images, ImagesUploader
  serialize :images, JSON
  ...
end

class ItemsController < ApplicationController
  ...
  item.remote_images_urls = [params[:image_url]]
  ...
end

Mind the right plural types in the helper method as well as the surrounding array for the params (carrierwave excpects an array).

You need to use item.remote_images_urls not item.remote_images_url and assign the array of remote url. This works for me although CarrierWave rubydoc suggests me to use item.remote_image_urls . So, maybe you should try one of these .

For reference CarrierWave rubydoc

Related