# Variables visible in all actions in a controller

**URL:** <https://www.ruby-forum.com/t/variables-visible-in-all-actions-in-a-controller/149176>\
**Category:** Rails\
**Created:** [September 22, 2008, 9:06am UTC](https://www.ruby-forum.com/t/variables-visible-in-all-actions-in-a-controller/149176 "2008-09-22T09:06:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jaikishan\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ad7895/32.png) [@Jaikishan\_J](https://www.ruby-forum.com/u/Jaikishan_J)\
**Post date:** [September 22, 2008, 9:06am UTC](https://www.ruby-forum.com/t/variables-visible-in-all-actions-in-a-controller/149176/1 "2008-09-22T09:06:08Z")

</div>

Hello,  
I want make a variable visible to all actions in a controller. Everytime  
user submits a parameter “school\_class\_id” on the basis of which I  
assign a  
variable which I want to make available to all the actions in  
controller.

I used before\_filter: set\_class\_id where set\_class\_id is defined as

def set\_class\_id  
@currentClassid = session[:currentclassid]  
if !params[:class\_list].nil?  
if !params[:class\_list][:class\_id].nil?  
@currentClassid = params[:class\_list][:class\_id]  
end  
end  
end

In this case, value of @currentClassid changes in index action. However  
in  
other actions, it remains the same as the value it is assigned intially  
for  
the first time ( In this case , the value in the session ). I am  
clueless  
why this is happening. If in index action, the value of @currentClassid  
is  
changing, then why not in other actions?

–  
Thanks,  
Jaikishan

“Enjoying the rights of being intelligent ignorant”

---

<div class="post-metadata">

**Author:** ![Jaikishan\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ad7895/32.png) [@Jaikishan\_J](https://www.ruby-forum.com/u/Jaikishan_J)\
**Post date:** [September 22, 2008, 9:19am UTC](https://www.ruby-forum.com/t/variables-visible-in-all-actions-in-a-controller/149176/2 "2008-09-22T09:19:37Z")

</div>

Jaikishan J. wrote:

> ```
> if !params[:class_list][:class_id].nil?
> @currentClassid = params[:class_list][:class_id]
> end
> 
> ```
> 
> end  
> end
> 
> In this case, value of @currentClassid changes in index action. However in  
> other actions, it remains the same as the value it is assigned intially for  
> the first time ( In this case , the value in the session ). I am clueless  
> why this is happening. If in index action, the value of @currentClassid is  
> changing, then why not in other actions?

How could it change? Instance variables (by definition) are restricted  
to an instance of a class. When you view another action the filter  
runs again and @currentClassid is set to session[:currentclassid]. If  
you do want it to persist across actions you could put in back in the  
session after setting it.

---

<div class="post-metadata">

**Author:** ![Jaikishan\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ad7895/32.png) [@Jaikishan\_J](https://www.ruby-forum.com/u/Jaikishan_J)\
**Post date:** [September 22, 2008, 9:20am UTC](https://www.ruby-forum.com/t/variables-visible-in-all-actions-in-a-controller/149176/3 "2008-09-22T09:20:13Z")

</div>

On Mon, Sep 22, 2008 at 9:05 AM, Jaikishan J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> end  
> end  
> In this case, value of @currentClassid changes in index action. However in  
> other actions, it remains the same as the value it is assigned intially for  
> the first time ( In this case , the value in the session ). I am clueless  
> why this is happening. If in index action, the value of @currentClassid is  
> changing, then why not in other actions?

If that filter runs in all actions, and the params are received, the  
instance variable should store what’s in params. I guess in actions  
other than index the params are not being sent.

A note on conventions: in Ruby instance variables use snake case:  
@current\_class\_id.
