# Setting custom variables per environment

**URL:** <https://www.ruby-forum.com/t/setting-custom-variables-per-environment/114683>\
**Category:** Rails\
**Created:** [September 17, 2007, 1:24pm UTC](https://www.ruby-forum.com/t/setting-custom-variables-per-environment/114683 "2007-09-17T13:24:06Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![eggie5](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/7c8e57/32.png) [@eggie5](https://www.ruby-forum.com/u/eggie5)\
**Post date:** [September 17, 2007, 1:24pm UTC](https://www.ruby-forum.com/t/setting-custom-variables-per-environment/114683/1 "2007-09-17T13:24:06Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![eggie5](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/7c8e57/32.png) [@eggie5](https://www.ruby-forum.com/u/eggie5)\
**Post date:** [September 17, 2007, 1:24pm UTC](https://www.ruby-forum.com/t/setting-custom-variables-per-environment/114683/2 "2007-09-17T13:24:58Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![eggie5](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/7c8e57/32.png) [@eggie5](https://www.ruby-forum.com/u/eggie5)\
**Post date:** [September 17, 2007, 1:25pm UTC](https://www.ruby-forum.com/t/setting-custom-variables-per-environment/114683/3 "2007-09-17T13:25:16Z")

</div>

One slight mistake, thats controllers/application.rb not  
application\_controller.rb.

William P. wrote:

> eggie5 wrote:
> 
> >

–  
Sincerely,

William P.

---

<div class="post-metadata">

**Author:** ![eggie5](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/7c8e57/32.png) [@eggie5](https://www.ruby-forum.com/u/eggie5)\
**Post date:** [September 25, 2007, 11:04pm UTC](https://www.ruby-forum.com/t/setting-custom-variables-per-environment/114683/4 "2007-09-25T23:04:48Z")

</div>

what does the .freeze do?

---

<div class="post-metadata">

**Author:** ![eggie5](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/7c8e57/32.png) [@eggie5](https://www.ruby-forum.com/u/eggie5)\
**Post date:** [September 25, 2007, 11:00pm UTC](https://www.ruby-forum.com/t/setting-custom-variables-per-environment/114683/5 "2007-09-25T23:00:26Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![eggie5](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/7c8e57/32.png) [@eggie5](https://www.ruby-forum.com/u/eggie5)\
**Post date:** [September 25, 2007, 11:04pm UTC](https://www.ruby-forum.com/t/setting-custom-variables-per-environment/114683/6 "2007-09-25T23:04:59Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![eggie5](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/7c8e57/32.png) [@eggie5](https://www.ruby-forum.com/u/eggie5)\
**Post date:** [September 25, 2007, 11:05pm UTC](https://www.ruby-forum.com/t/setting-custom-variables-per-environment/114683/7 "2007-09-25T23:05:03Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![eggie5](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/7c8e57/32.png) [@eggie5](https://www.ruby-forum.com/u/eggie5)\
**Post date:** [September 25, 2007, 11:05pm UTC](https://www.ruby-forum.com/t/setting-custom-variables-per-environment/114683/8 "2007-09-25T23:05:37Z")

</div>

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\>

---

<div class="post-metadata">

**Author:** ![eggie5](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/7c8e57/32.png) [@eggie5](https://www.ruby-forum.com/u/eggie5)\
**Post date:** [September 25, 2007, 11:10pm UTC](https://www.ruby-forum.com/t/setting-custom-variables-per-environment/114683/9 "2007-09-25T23:10:50Z")

</div>

Will I be able to access this form a model?
