1

I'm using rails 3 and I would like to set the storage variable based on the current environment, like below:

 #if enviroment = development or test 
    :storage => :filesystem,
    #else
    :storage => :s3,
    #end

What would your approach be? I've thought about setting an environment variable, but I think that there must be a better way.

Thanks for your time Simone

2 Answers 2

9

One way to do this is use the built-in environment setup in rails. The variables set in the environments directory will override the config/environment

config/environment.rb

ATTACHMENT_OPTIONS = {:storage => :filesystem}

config/environments/production.rb

ATTACHMENT_OPTIONS = {:storage => :s3}

Then later in your app, just ask ATTACHMENT_OPTIONS[:storage] -- no if/else throughout your app.

Sign up to request clarification or add additional context in comments.

Comments

8

Rails.env will give you what you're looking for.

You can also use

Rails.env.development?
Rails.env.test?
Rails.env.production?
Rails.env.your_custom_environment?

To test for those specific environments.

3 Comments

Thank you! I have to put the test inside a model, and with Netbeans I always got an error. Here the code: has_attached_file :avatar, :path => ":attachment/:id/:style.:extension", :url => "/:attachment/:id/:style.:extension", :default_url => '/:attachment/missing_:style.jpg', :styles => { :small => "80x80>", :thumb => "100x100>" }, if (Rails.env.development? or Rails.env.test?) :storage => :filesystem else :storage => :s3 end :default_style => :thumb, :bucket => 'josha_pictures', :s3_credentials => { :access_key_id => ENV['S3_KEY'], :
What's the error? and would you mind editing your question and putting the code there?
why not use ternary operator instead of if/else? :storage => (Rails.env.development? || Rails.env.test?) ? :filesystem : :s3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.