【问题标题】:Carrierwave Multiple File Upload + No implicit conversion of nil into StringCarrierwave 多文件上传 + 没有将 nil 隐式转换为字符串
【发布时间】:2016-01-13 23:24:31
【问题描述】:

尝试让 Carrierwave 的多个文件上传正常工作。我正在关注主页上的文档。当我尝试上传一个或多个文件时,我得到一个no implicit conversion of nil to string

该错误来自 uploaders/cache.rb 中的 Carrierwave gem 中的此方法

def workfile_path(for_file=original_filename)
    File.join(CarrierWave.tmp_path, @cache_id, version_name.to_s, for_file)
 end

问题是 original_file 为零。我试图追踪问题,但找不到问题真正开始的地方。奇怪的一件事是我正在关注这个 repo 中的一些源代码

https://github.com/bobintornado/sample-gallery-app-with-carrierwave

示例应用正在运行,您可以进行多次上传。但不同之处在于,当调用 cache! 方法时,new_fileArray,而在运行的示例应用程序中,它是 Http::UploadedFile

这里是缓存方法

  def cache!(new_file = sanitized_file)
    new_file = CarrierWave::SanitizedFile.new(new_file)
    return if new_file.empty?

    raise CarrierWave::FormNotMultipart if new_file.is_path? && ensure_multipart_form

    self.cache_id = CarrierWave.generate_cache_id unless cache_id

    @filename = new_file.filename
    self.original_filename = new_file.filename

    begin
      # first, create a workfile on which we perform processings
      if move_to_cache
        @file = new_file.move_to(File.expand_path(workfile_path, root), permissions, directory_permissions)
      else
        @file = new_file.copy_to(File.expand_path(workfile_path, root), permissions, directory_permissions)
      end

      with_callbacks(:cache, @file) do
        @file = cache_storage.cache!(@file)
      end
    ensure
      FileUtils.rm_rf(workfile_path(''))
    end
  end

这是我的初始参数

"coach"=>{"name"=>"ben", "title"=>"ceo", "description"=>"head dude",
"photos"=>[
    #<ActionDispatch::Http::UploadedFile:0x007fc9a5235c78 @tempfile=#<Tempfile:/var/folders/sb/t6rry5j928l3sy96nkhy9f840000gn/T/RackMultipart20160113-67635-avg8ef.jpg>, @original_filename="benn-1.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"coach[photos][]\"; filename=\"benn-1.jpg\"\r\nContent-Type: image/jpeg\r\n">,
    #<ActionDispatch::Http::UploadedFile:0x007fc9a5235c50 @tempfile=#<Tempfile:/var/folders/sb/t6rry5j928l3sy96nkhy9f840000gn/T/RackMultipart20160113-67635-r8bdxp.jpg>, @original_filename="benn-2.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"coach[photos][]\"; filename=\"benn-2.jpg\"\r\nContent-Type: image/jpeg\r\n">
]}

【问题讨论】:

    标签: ruby-on-rails-4 carrierwave


    【解决方案1】:

    抱歉,这不是很有帮助,但我烧毁了我当前的工作分支并从头开始,现在一切正常。不知道我做了什么不同的事情。如果您遇到同样的问题,我建议您重新开始并遵循本教程:

    Multiple-Images-Uploading-With-CarrierWave-and-PostgreSQL-Array

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多