【问题标题】:Additional fields in CarrierWave UploaderCarrierWave Uploader 中的其他字段
【发布时间】:2015-04-18 19:18:08
【问题描述】:

我正在尝试向CarrierWave Uploader 添加其他字段,以便它们作为 Uploader 本身的一部分并与 CarrierWave 字段一起存储,例如 @file@model@storage 等。

这些字段也是特定于版本的,这就是为什么我希望能够通过<my_model>.<my_uploader>.attribute
<my_model>.<my_uploader>.versions[:<the_version>] 访问它们,而不是模型中的其他列。

我确实尝试了carrierwave-meta gem,但遇到了错误(NoMethodError: undefined method \'original_filename' for #<CarrierWave::Storage::Fog::File:0xab4134c>) 这似乎还没有解决。

关于如何最好地完成此任务的任何想法或建议?

【问题讨论】:

    标签: ruby-on-rails carrierwave


    【解决方案1】:

    我不是 100% 清楚你要做什么。

    当我使用carrierwave gem 时,我会创建一个包含一些信息的路径。在我的应用程序中,我通常有一个文件 app/uploaders/image_uploader.rb

      class ImageUploader < CarrierWave::Uploader::Base
        include CarrierWave::RMagick
        def store_dir
          # "uploads/image/file/187/"
          "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
        end
        ...
      end
    

    从这里我总是知道模型、什么类型的文件和 id。 我通常将有关此模型的所有其他信息保存在数据库中。

    我希望这对您有所帮助并为您指明正确的方向

    【讨论】:

      【解决方案2】:

      您的错误与雾有关

      在我的Picture Uploader 中我可以设置属性读取器和写入器

      class PictureUploader < CarrierWave::Uploader::Base
        include CarrierWave::MiniMagick
        storage :file
      
        def field
          @field
        end
      
        def field=(field)
          @field = field
        end    
      
        # attr_accessor :field # for an even shorter way
      end
      

      我打开rails console测试模型:

      picture = PictureUploader.new 
      => #<PictureUploader:0x0055804db336e8 @model=nil, @mounted_as=nil>
      
      picture.field=('your text')
      => "your text"
      
      picture.field
      "your text"
      

      关于您遇到的版本控制和错误'NoMethodError: undefined method \'original_filename' for #&lt;CarrierWave::Storage::Fog::File:0xab4134c&gt;' 我同意MZaragoza

      CarrierWave::Storage::Fog::File.new 接受三个参数

        def store!(file)
          f = CarrierWave::Storage::Fog::File.new(uploader, self, uploader.store_path)
          f.store(file)
          f
        end
      

      uploaderselfuploader.store_path 为了帮助我们解决这个问题,您应该包括您的 CarrierwaveUploader 模型代码和 uploader.store_path 的输出

      非常感谢

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-02-03
        • 1970-01-01
        • 1970-01-01
        • 2023-03-13
        • 1970-01-01
        • 1970-01-01
        • 2018-04-23
        • 2020-07-03
        相关资源
        最近更新 更多