lardawge/carrierwave_backgrounder

No version images available after save, only original is uploaded

Closed this issue · 2 comments

I am using carrierwave_backgrounder, sidekiq and mongoid. My model defines events embed many image.

Here is my uploader:

class EventImageUploader < CarrierWave::Uploader::Base
  include ::CarrierWave::Backgrounder::Delay
  include CarrierWave::MiniMagick

  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{model.id}/#{mounted_as}"
  end

  def default_url
    # carrierwave backgrounder store_in_backgroud sets image_tmp
    # to the filename in the cache_path. Following default_url
    # serves the cached file before image is stored to s3, making
    # the image shows up earlier in the client
    "/#{cache_dir}/#{model.image_tmp}"
  end

  version :m do
            process :resize_to_fill => [400, 400]
   end
end

And my image Class

class Image do
      include Mongoid::Document
      include Mongoid::Timestamps::Created::Short

     store_in_background :image, ::ImageWorker
     field :image_tmp, type: String

      mount_uploader :image, "EventImageUploader"

      embedded_in :event
end

However when I do upload, only my original image has been uploaded. When I inspect new_image.image.m.url, I got "/uploads/tmp/".

Whereas new_image.image.url gives "/uploads/tmp/1484644438-44299-6173/image.jpg".

@jeffrey008 Hey, how did you solve your problem? I'm facing the exactly same issue right now.

HI @michalgritzbach.
In your model, you should do mount_uploader first, and then do store_in_background.
Example:

class Image do
  include Mongoid::Document

  mount_uploader :image, "EventImageUploader"
  store_in_background :image, ::ImageWorker
  field :image_tmp, type: String

  embedded_in :event
end

Also make sure you defined your versions in your uploaders/event_image_uploader.rb.

class EventImageUploader
  version :s
  version :m
end

Post your code if things still doesn't work.