我在app中使用了carrierwave来上传声音。
我使用大文件,所以我配置我的carrierwave使用move_to_cache和move_to_store,但问题是我现在上传文件,在我的缓存文件夹carrierwave create 2文件夹中,我的商店有两个cache.id和1个文件夹。
当我上传文件时,我需要在缓存文件夹中有1个文件夹,在我的商店文件夹中有1个文件夹。在我的缓存文件夹中,我希望我的文件被删除,但实际上我的文件保留在我的缓存文件夹中。
我希望我很清楚。
我告诉你我的sound_uploader.rb
class SoundUploader < CarrierWave::Uploader::Base
before :store , :print
def print(new_file)
puts ("PRINT CAAAAACHE")
puts (cache_id)
end
# Choose what kind of storage to use for this uploader:
storage :file
def store_dir
"tmp/#{model.class.to_s.underscore}/store/#{model.id}"
end
def cache_dir
"tmp/#{model.class.to_s.underscore}/cache/#{model.id}"
end
def move_to_cache
puts("MOVE TO CACHE ")
false
end
def move_to_store
puts("MOVE TO STORE ")
true
end
def extension_white_list
%w(3ga 3gp 3g2 3gpp 3gp2 m4a m4b m4p m4v m4r mp4 aac flac flv avi asf wma wmv dpx mkv mka mks bwf mpg mpeg mp1 mp2 mp3
m1v m1a m2a mpa mpv rm mov ogm ogg ogv oga ogx nut riff webm weba wav mxf asx ts aiff aif aifc au snd caf)
end
def filename
model.title = original_filename if model.title.to_s == ''
"#{secure_token}.#{file.extension}" if original_filename.present?
end
protected
def secure_token
var = :"@#{mounted_as}_secure_token"
model.instance_variable_get(var) or model.instance_variable_set(var, SecureRandom.uuid)
end
end
谢谢你的帮助。
答案 0 :(得分:3)
可能的原因是您正在使用cancan的load_and_authorize_resource
或load_resource,而您忘记在控制器操作(@song = Song.new(params[:song])
中创建模型对象,因此资源被创建两次,这会导致carrierwave将文件移动到缓存两次。