在 CarrierWave 中,retrieve_from_store!做?

In CarrierWave, what does retrieve_from_store! do?

documentationretrieve_from_store!:

Retrieves the file from the storage.

但是当我调用该方法时,我并没有得到类似文件的东西,而是得到了一个返回的数组:

irb(main):008:0> uploader.retrieve_from_store!('my_file.png')
=> [:retrieve_versions_from_store!]

该方法具体是做什么的?

我今天也在找同样的东西。在 CarrierWave 论坛 here...

中找到了来自 Jonas Nicklas 的内容

retrieve_from_store! changes the state of the uploader, it doesn't return anything sensible. You want to do this:

uploader.retrieve_from_store!('test.jpg')
uploader.do_whatever

The return value from retrieve_from_store! is irrelevant.

Carrierwave 的设计不适合没有模型的情况。

class DocumentUploader < CarrierWave::Uploader::Base
  storage :file

  def store!(*args)
    super
    @file
  end

  def filename
    SecureRandom.uuid
  end
end

file = uploader.store! file
puts file.path # ok

您可以用同样的方式修补 retrieve_from_store

其他方法获取重要信息比较麻烦,请尽量使用其他上传器。

我只需要使用 model.uploader.read 来获取字节。看来,上传者通过 Proxy 委托文件:https://github.com/carrierwaveuploader/carrierwave/blob/master/lib/carrierwave/uploader/proxy.rb#L43