lundi 20 avril 2015

Carrierwave - Save nil if the image is not uploaded

I want to save nil value in database if the image is not uploaded.

My uploader:

class Avatar < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
storage: file
def store_dir
    if model
      "uploads/#{model.class.to_s.underscore}/#{model.id}/#{mounted_as}"
    else
      "uploads/#{mounted_as}/"
    end
 end 
end

console output:

>Article.first.avatar
>true
#<AvatarUploader:0x007f9813f1fe70
 @file=
  #<CarrierWave::SanitizedFile:0x007f9813f1e688
   @content_type=nil,
   @file="/Users/work/project/app1/public/uploads/370/avatar/avatar.png",
   @original_filename=nil>,
 @model=
  ##Article model
 @mounted_as=:avatar,
 @storage=#<CarrierWave::Storage::File:0x007f9813f1fad8 @uploader=#<AvatarUploader:0x007f9813f1fe70 ...>>,
 @versions={}>

How to save nil value in database if an image is not uploaded

Aucun commentaire:

Enregistrer un commentaire