# Controller-wide instance variable

**URL:** <https://www.ruby-forum.com/t/controller-wide-instance-variable/62497>\
**Category:** Rails\
**Created:** [June 5, 2006, 5:21pm UTC](https://www.ruby-forum.com/t/controller-wide-instance-variable/62497 "2006-06-05T17:21:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ryan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a8b319/32.png) [@ryan](https://www.ruby-forum.com/u/ryan)\
**Post date:** [June 5, 2006, 5:21pm UTC](https://www.ruby-forum.com/t/controller-wide-instance-variable/62497/1 "2006-06-05T17:21:07Z")

</div>

Is it possible to declare an instance variable in a controller that is  
available to every action without defining the variable in every action?

---

<div class="post-metadata">

**Author:** ![ryan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a8b319/32.png) [@ryan](https://www.ruby-forum.com/u/ryan)\
**Post date:** [June 5, 2006, 5:29pm UTC](https://www.ruby-forum.com/t/controller-wide-instance-variable/62497/2 "2006-06-05T17:29:05Z")

</div>

On 6/5/06, ryan [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Is it possible to declare an instance variable in a controller that is  
> available to every action without defining the variable in every action?

You have two main options (though there are others):

The firstis to use a session variable:

session[:name] = value

The other is a “class variable”:

@@variable = value

That will create a “class variable” which is available to every instance  
of  
the controller. However, because the Rails environment will get reloaded  
on  
each request in development mode, the value won’t persist across  
requests.  
In production mode, I think the behavior is indeterminate and depends on  
your setup.

---

<div class="post-metadata">

**Author:** ![ryan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a8b319/32.png) [@ryan](https://www.ruby-forum.com/u/ryan)\
**Post date:** [June 5, 2006, 5:32pm UTC](https://www.ruby-forum.com/t/controller-wide-instance-variable/62497/3 "2006-06-05T17:32:44Z")

</div>

ryan wrote:

> Is it possible to declare an instance variable in a controller that is  
> available to every action without defining the variable in every action?

You could do it in a before\_filter.

–  
Jack C.  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![ryan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a8b319/32.png) [@ryan](https://www.ruby-forum.com/u/ryan)\
**Post date:** [June 5, 2006, 6:10pm UTC](https://www.ruby-forum.com/t/controller-wide-instance-variable/62497/4 "2006-06-05T18:10:06Z")

</div>

Ryan:

Consider using before\_filter instead of initialize (It’s a good habit  
and you may want to do something else in initialize, or use the same  
code over and over)

application.rb

def do\_stuff  
@variable\_to\_display = “hello world”  
end

store\_contrller.rb

```
before_filter :do_stuff

```

login\_controller.rb

before\_filter :do\_stuff, :except=\>[:login, :logout]

---

<div class="post-metadata">

**Author:** ![ryan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a8b319/32.png) [@ryan](https://www.ruby-forum.com/u/ryan)\
**Post date:** [June 5, 2006, 6:16pm UTC](https://www.ruby-forum.com/t/controller-wide-instance-variable/62497/5 "2006-06-05T18:16:45Z")

</div>

Thanks for the input. I suppose before\_filter would be more flexible  
than initialize.

Brian H. wrote:

> Ryan:
> 
> Consider using before\_filter instead of initialize (It’s a good habit  
> and you may want to do something else in initialize, or use the same  
> code over and over)
> 
> application.rb
> 
> def do\_stuff  
> @variable\_to\_display = “hello world”  
> end
> 
> store\_contrller.rb
> 
> ```
> before_filter :do_stuff
> 
> ```
> 
> login\_controller.rb
> 
> before\_filter :do\_stuff, :except=\>[:login, :logout]

---

<div class="post-metadata">

**Author:** ![ryan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a8b319/32.png) [@ryan](https://www.ruby-forum.com/u/ryan)\
**Post date:** [June 5, 2006, 5:33pm UTC](https://www.ruby-forum.com/t/controller-wide-instance-variable/62497/6 "2006-06-05T17:33:58Z")

</div>

Thanks for the help.

I just found what I wanted. I didn’t need persistence across actions,  
but actually to just repeat some code for every action (but I didn’t  
want to actually duplicate code.)

I used the initialize() fuction in the controller class.

Justin B. wrote:

> On 6/5/06, ryan [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Is it possible to declare an instance variable in a controller that is  
> > available to every action without defining the variable in every action?
> 
> You have two main options (though there are others):
> 
> The firstis to use a session variable:
> 
> session[:name] = value
> 
> The other is a “class variable”:
> 
> @@variable = value
> 
> That will create a “class variable” which is available to every instance  
> of  
> the controller. However, because the Rails environment will get reloaded  
> on  
> each request in development mode, the value won’t persist across  
> requests.  
> In production mode, I think the behavior is indeterminate and depends on  
> your setup.
