Wednesday, January 3, 2018

Additional fields in CarrierWave Uploader

Leave a Comment

I am trying to add additional fields to the CarrierWave Uploader so that they are stored as part of the Uploader itself and together with the CarrierWave fields, such as @file, @model, @storage etc.

The fields are also version-specific, which is why I'd prefer to be able to access them via <my_model>.<my_uploader>.attribute and
<my_model>.<my_uploader>.versions[:<the_version>] instead of additional columns in the model.

I did try the carrierwave-meta gem, but ran into an error with it ( NoMethodError: undefined method \'original_filename' for #<CarrierWave::Storage::Fog::File:0xab4134c> ) that seems to not have been fixed yet.

Any ideas or suggestions on how to best accomplish this?

2 Answers

Answers 1

I'm not 100% clear what you are trying to do.

when I use carrierwave gem, I do create a path that holds some of that information. In my applaications I normally have a file 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 

from this I always know the model, what type of file, and the id. All other info about this model I normally save in the database.

I hope this helps and sets you in the right direction

Answers 2

your error is connected with fog

In my Picture Uploader I can set an attribute reader and writer

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 

The I open the rails console to test the model:

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

About the versioning and error you are having 'NoMethodError: undefined method \'original_filename' for #<CarrierWave::Storage::Fog::File:0xab4134c>' I agree with MZaragoza

CarrierWave::Storage::Fog::File.new takes three parameters

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

uploader, self and uploader.store_path so to help us solve this problem you should include your CarrierwaveUploader model code and the output of uploader.store_path

Thanks a lot

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment