# Howto add application-wide variables?

**URL:** <https://www.ruby-forum.com/t/howto-add-application-wide-variables/56937>\
**Category:** Rails\
**Created:** [April 5, 2006, 11:16pm UTC](https://www.ruby-forum.com/t/howto-add-application-wide-variables/56937 "2006-04-05T23:16:10Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![aginghipster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@aginghipster](https://www.ruby-forum.com/u/aginghipster)\
**Post date:** [April 5, 2006, 11:16pm UTC](https://www.ruby-forum.com/t/howto-add-application-wide-variables/56937/1 "2006-04-05T23:16:10Z")

</div>

I suspect this is something I should know already but I can’t seem to  
get it right, how can I create application wide variables and have them  
accessible to all my controllers?

I tried creating a plugin and having my controller require a specific  
method from that plugin to access its variables but that didn’t work, so  
I am left scratching my head.

eg from plugin ./vendor/plugins/myplugin/shared\_defs.rb file

Module SharedDefs  
def load\_shared\_defs  
@var1 = something  
@var2 = something\_else  
end  
end

eg from controller  
include SharedDefs  
require :load\_shared\_defs

def index  
@my\_stuff = @var1  
end

@var1 is not defined and so my guess is my scope is off or I’m not  
accessing it correctly but I am not sure which.

Thanks in advance for any light you can shed! 🙂

-Andy

---

<div class="post-metadata">

**Author:** ![aginghipster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@aginghipster](https://www.ruby-forum.com/u/aginghipster)\
**Post date:** [April 6, 2006, 1:15am UTC](https://www.ruby-forum.com/t/howto-add-application-wide-variables/56937/2 "2006-04-06T01:15:56Z")

</div>

Cool !!

thanx

---

<div class="post-metadata">

**Author:** ![aginghipster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@aginghipster](https://www.ruby-forum.com/u/aginghipster)\
**Post date:** [April 6, 2006, 12:32am UTC](https://www.ruby-forum.com/t/howto-add-application-wide-variables/56937/3 "2006-04-06T00:32:26Z")

</div>

If you ever need to share something between multiple controllers, it  
often goes in the ApplicationController (application.rb) class. Since  
all controllers inherit from this class, they will inherit any  
instance variables set. You can set a method up as a “before\_filter”  
so it is called before each action to prepare any variables. For  
example:

# application.rb

class ApplicationController \< ActionController::Base  
before\_filter :prepare\_variables

def prepare\_variables  
@var1 = something  
@var2 = something\_else  
end  
end

However, design-wise, there may be better alternatives depending upon  
what you are trying to accomplish. For example, if the variable  
doesn’t change, it may be better to use a constant. Or, if you are  
just setting up some default variables that the controller can  
override, you may be able to use logic in the view to handle this.  
For example, instead of this:

# application.rb

class ApplicationController \< ActionController::Base  
before\_filter :prepare\_variables

def prepare\_variables  
@title = ‘Default Title’  
end  
end

You can place the default in the view like this:

# application.rhtml

…

\<%= @title || 'Default Title' %\> ...

This would display ‘Default Title’ as the page title if the  
controller hasn’t set the @title instance variable.

Hope that helps.

Ryan

---

<div class="post-metadata">

**Author:** ![aginghipster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@aginghipster](https://www.ruby-forum.com/u/aginghipster)\
**Post date:** [April 6, 2006, 3:09am UTC](https://www.ruby-forum.com/t/howto-add-application-wide-variables/56937/4 "2006-04-06T03:09:54Z")

</div>

On 6/04/2006, at 9:10 AM, Andrew G. Cowan wrote:

> I suspect this is something I should know already but I can’t seem  
> to get it right, how can I create application wide variables and  
> have them accessible to all my controllers?

While the solutions posted are fine for constant values, if you want  
to change the values during runtime they will not work when you have  
to scale up your application. Use the database.

–  
Phillip H.  
removed\_email\_address@domain.invalid  
[http://www.sitharus.com/](http://www.sitharus.com/)

---

<div class="post-metadata">

**Author:** ![aginghipster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@aginghipster](https://www.ruby-forum.com/u/aginghipster)\
**Post date:** [April 6, 2006, 5:33pm UTC](https://www.ruby-forum.com/t/howto-add-application-wide-variables/56937/5 "2006-04-06T17:33:52Z")

</div>

On Apr 5, 2006, at 6:06 PM, Phillip H. wrote:

> While the solutions posted are fine for constant values, if you  
> want to change the values during runtime they will not work when  
> you have to scale up your application. Use the database.

Could you explain this a little more? Are you referring to setting  
instance variables in the ApplicationController? I’m curious why this  
wouldn’t work when scaling up the application. Thanks.

One more thing I forgot to include in my previous example. If you use  
a before\_filter, you should make the method private so it is not  
considered an action. See the documentation for an example.

[http://api.rubyonrails.com/classes/ActionController/Filters/](http://api.rubyonrails.com/classes/ActionController/Filters/)  
ClassMethods.html

Ryan

---

<div class="post-metadata">

**Author:** ![aginghipster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@aginghipster](https://www.ruby-forum.com/u/aginghipster)\
**Post date:** [April 8, 2006, 12:28pm UTC](https://www.ruby-forum.com/t/howto-add-application-wide-variables/56937/6 "2006-04-08T12:28:33Z")

</div>

Ryan B. wrote:

> On Apr 5, 2006, at 6:06 PM, Phillip H. wrote:
> 
> > While the solutions posted are fine for constant values, if you want  
> > to change the values during runtime they will not work when you have  
> > to scale up your application. Use the database.
> 
> Could you explain this a little more? Are you referring to setting  
> instance variables in the ApplicationController? I’m curious why this  
> wouldn’t work when scaling up the application. Thanks.

If you go beyond using WEBrick, you will be using a technology such as  
FastCGI which has a pool of request-handling processes. Changing a value  
in memory will only have an effect in the process in which the change  
happens - the other processes won’t see the change.

So to share a variable across the application it must be in the  
database, or in the filestore, or in a separate shared service (e.g.  
using Distributed Ruby).

regards

Justin

---

<div class="post-metadata">

**Author:** ![aginghipster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@aginghipster](https://www.ruby-forum.com/u/aginghipster)\
**Post date:** [April 6, 2006, 1:53am UTC](https://www.ruby-forum.com/t/howto-add-application-wide-variables/56937/7 "2006-04-06T01:53:45Z")

</div>

This is exactly the information I was looking, thank you so much for  
pointing this out Ryan! 🙂

-Andy

Ryan B. wrote:

> If you ever need to share something between multiple controllers, it  
> often goes in the ApplicationController (application.rb) class. Since  
> all controllers inherit from this class, they will inherit any  
> instance variables set. You can set a method up as a “before\_filter”  
> so it is called before each action to prepare any variables. For  
> example:
> 
> # application.rb
> 
> class ApplicationController \< ActionController::Base  
> before\_filter :prepare\_variables
> 
> def prepare\_variables  
> @var1 = something  
> @var2 = something\_else  
> end  
> end
> 
> However, design-wise, there may be better alternatives depending upon  
> what you are trying to accomplish. For example, if the variable  
> doesn’t change, it may be better to use a constant. Or, if you are  
> just setting up some default variables that the controller can  
> override, you may be able to use logic in the view to handle this.  
> For example, instead of this:
> 
> # application.rb
> 
> class ApplicationController \< ActionController::Base  
> before\_filter :prepare\_variables
> 
> def prepare\_variables  
> @title = ‘Default Title’  
> end  
> end
> 
> You can place the default in the view like this:
> 
> # application.rhtml
> 
> …
> 
> \<%= @title || 'Default Title' %\> ...
> 
> This would display ‘Default Title’ as the page title if the  
> controller hasn’t set the @title instance variable.
> 
> Hope that helps.
> 
> Ryan
