开发者

Setting a variable based on Rails environment

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

开发者_如何学Python #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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜