21

I am not sure how to configure the environment such that Carrier Wave will use the local file storage when running the app locally (development) and s3 after i load to heroku (production)

in Development storage :file

in Production storage :s3

user663778
  • 381
  • 4
  • 14

3 Answers3

24

Either model, or you can set it globally. Have a look at the readme for v0.5.2 (current gem) at https://github.com/jnicklas/carrierwave/tree/v0.5.2

Near the bottom, there are some instructions for configuring the test environment. Use the same approach to use different configurations for "development" and "production", e.g. add a file "carrierwave.rb" to "config/initialisers" and add the configuration code

if Rails.env.test? or Rails.env.cucumber?
  CarrierWave.configure do |config|
    config.storage = :file
    config.enable_processing = false
  end
end

and for development

if Rails.env.development?
  CarrierWave.configure do |config|
    config.storage = :file
  end
end

and production

if Rails.env.production?
  CarrierWave.configure do |config|
    config.storage = :s3
  end
end
juwalter
  • 11,472
  • 5
  • 19
  • 18
  • 1
    in which file do i put this code? Development.rb? I put it in development.rb and i get an error message can't convert Symbol into Integer when i try and access avatar.url? – user663778 Mar 20 '11 at 22:15
  • 2
    I would put it in "config/initializers/carrierwave.rb" (create a new file). – juwalter Mar 21 '11 at 07:36
8

The simplest way I usually do is through the Uploader.

class CoverUploader < CarrierWave::Uploader::Base
  # Choose what kind of storage to use for this uploader:
  storage (Rails.env.production? ? :fog : :file)
end
Jason Torres
  • 81
  • 1
  • 2
  • Thanks for the succinct one liner. This seems like a better place (in the uploader) to make this environment-based storage decision. In my case I only needed to make a distinction in one of my uploaders. – Robert Travis Pierce Oct 05 '16 at 14:09
1

I'm guessing this is set in a model somewhere. You could do something like

if Rails.env.production?
  // set production
else
  // set dev / test
end
Dogbert
  • 212,659
  • 41
  • 396
  • 397