Setting custom variables per environment

Hi,

I’d like to be able to set a variable for each environment I have.
It’s a path to a folder that’s different on each system: development
and production. I then, need to be able to read it from my controller.

How can I do this?

You can set it in application_controller.rb like this

case ENV[‘RAILS_ENV’]
when ‘production’ : var = ‘foo’
when ‘development’ : var = ‘bar’
…etc
end

eggie5 wrote:


Sincerely,

William P.

One slight mistake, thats controllers/application.rb not
application_controller.rb.

William P. wrote:

eggie5 wrote:


Sincerely,

William P.

what does the .freeze do?

Sorry, I am really not paying attention…if you want to access this
from another controller other than ApplicationController, you need to
make it a class variable like @@var.

William P. wrote:

I put the exact code you wrote in my controllers/application.rb file,

One slight mistake, thats controllers/application.rb not

when 'development' : var = 'bar'


Sincerely,

William P.


Sincerely,

William P.

sorry, I should have been more specific:

case ENV[‘RAILS_ENV’]
when ‘production’ : @var = ‘foo’
when ‘development’ : @var = ‘bar’
end

You need to make the variable an instance variable. Sorry, long day and
I was getting lazy.

eggie5 wrote:

when 'production' : var = 'foo'

and production. I then, need to be able to read it from my controller.


Sincerely,

William P.

Will I be able to access this form a model?

No.

You could create a constant instead of a instance/class var by adding
this
into your config/environments/development.rb:

SOME_VAR = “foo”
SOME_VAR.freeze

Then in production.rb do the same, but set it to something else.

I put the exact code you wrote in my controllers/application.rb file,
then tried to access var on my other controller and it won’t work.:

logger.info “***** var is : #{var}”

undefined local variable or method `var’ for #<PrivateController:
0x3525be0>

Will I be able to access this form a model?

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs