I started using Carrierwave, and I found out that it stores temporary files at multiple places depending on whether it is testing or delayed job.
If it is done during testing(rspec), the temp files will be at RAILS_ROOT/uploads/tmp
directory.
If it is done during delayed job, the temp files will be at RAILS_ROOT/public/uploads/tmp
First, I was thinking that rails_root/tmp/uploads
would be a more sensible place, or even the system temporary folder.
Second, the testing being different to normal run seems like a bug.
Is there a way to fix this (either by configuration or monkey patching)? And can I put things in the RAILS_ROOT/tmp folder?
There is config.cache_dir
option you can set in 'config/initializers/carrierwave.rb'. But it is relative to '/public'. Looks like you should do that in your uploaders:
class MyUploader < CarrierWave::Uploader::Base
def cache_dir
# should return path to cache dir
Rails.root.join 'tmp/uploads'
end
end
Im using carrierwave 0.10.0 and It seems this behaviour was addressed. Now the cache_dir
setting accepts a path that is outside the public directory:
CarrierWave.configure do |config|
config.cache_dir = Rails.root.join 'tmp/uploads'
end
@iKindred's answer worked for me, but I thought I'd expand on how I got it to work on Rails 4.2.3 with CarrierWave 0.10.0:
Where to put the configure block
As CarrierWave is not part of Rails, the best place to put the configure block, according to my reading of the Rails Guide, is an initializer file. The name can be chosen arbitrarily, but it needs to reside in Rails.root/config/initializers
. I chose to name the file after the module I was configuring: Rails.root/config/initializers/carrier_wave.rb
.
What to put in the initializer file
I find that I run tests while my Rails app is running in development
mode. After the tests I clean up all files by unlinking the storage paths as described here. For that to work without messing with the files stored by the Rails app running in development
mode, storage paths need to be separated by environment! Therefore, my modified version of @iKindred's answer is:
#config/initializers/carrier_wave.rb
CarrierWave.configure do |config|
config.cache_dir = File.join(Rails.root, 'tmp', 'uploads', Rails.env)
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With