# Variable visible in all actions of the controller

**URL:** <https://www.ruby-forum.com/t/variable-visible-in-all-actions-of-the-controller/121497>\
**Category:** Rails\
**Created:** [November 28, 2007, 1:51pm UTC](https://www.ruby-forum.com/t/variable-visible-in-all-actions-of-the-controller/121497 "2007-11-28T13:51:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hipnotik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/41988e/32.png) [@Hipnotik](https://www.ruby-forum.com/u/Hipnotik)\
**Post date:** [November 28, 2007, 1:51pm UTC](https://www.ruby-forum.com/t/variable-visible-in-all-actions-of-the-controller/121497/1 "2007-11-28T13:51:58Z")

</div>

Hi  
I would like to have variable visible for all actions of one  
controller. It’s something like admin panel where I have some actions,  
such as ‘list’, ‘new’, ‘edit’ and I have to display at the top of the  
page logged user’s name.  
How and where do I have to declare and assign variable like this?

Thanks for help

---

<div class="post-metadata">

**Author:** ![Hipnotik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/41988e/32.png) [@Hipnotik](https://www.ruby-forum.com/u/Hipnotik)\
**Post date:** [November 28, 2007, 2:06pm UTC](https://www.ruby-forum.com/t/variable-visible-in-all-actions-of-the-controller/121497/2 "2007-11-28T14:06:44Z")

</div>

Hipnotik wrote:

> Hi  
> I would like to have variable visible for all actions of one  
> controller. It’s something like admin panel where I have some actions,  
> such as ‘list’, ‘new’, ‘edit’ and I have to display at the top of the  
> page logged user’s name.  
> How and where do I have to declare and assign variable like this?
> 
> Thanks for help

why not use acts\_as\_authenticated?

generally speaking, a session should suffice - that’s what most  
authentication systems use anyhow 😉

session[:user].name or something like that

hth,

–shai

---

<div class="post-metadata">

**Author:** ![Hipnotik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/41988e/32.png) [@Hipnotik](https://www.ruby-forum.com/u/Hipnotik)\
**Post date:** [November 28, 2007, 2:57pm UTC](https://www.ruby-forum.com/t/variable-visible-in-all-actions-of-the-controller/121497/3 "2007-11-28T14:57:44Z")

</div>

On Nov 28, 2:06 pm, Shai R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> why not use acts\_as\_authenticated?  
> Posted viahttp://www.ruby-forum.com/.  
> I would like to not use additional plugins to do that.  
> It works on sessions, of course, and I know that I can put user\_name  
> in session but I would like to keep only user\_id and read other  
> information from database. But I don’t want to do something like:  
> @user\_name = User.find\_by\_id(session[:user\_id])  
> in every action. I would like to do that once per controller and use  
> it in all views available for these actions.  
> Is it correct and possible?

---

<div class="post-metadata">

**Author:** ![Hipnotik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/41988e/32.png) [@Hipnotik](https://www.ruby-forum.com/u/Hipnotik)\
**Post date:** [November 28, 2007, 3:42pm UTC](https://www.ruby-forum.com/t/variable-visible-in-all-actions-of-the-controller/121497/4 "2007-11-28T15:42:53Z")

</div>

I guess you could write a helper method too, and use it in your view.

---

<div class="post-metadata">

**Author:** ![Hipnotik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/41988e/32.png) [@Hipnotik](https://www.ruby-forum.com/u/Hipnotik)\
**Post date:** [November 28, 2007, 3:22pm UTC](https://www.ruby-forum.com/t/variable-visible-in-all-actions-of-the-controller/121497/5 "2007-11-28T15:22:23Z")

</div>

On 28 Nov 2007, at 13:57, Hipnotik wrote:

> I would like to not use additional plugins to do that.  
> It works on sessions, of course, and I know that I can put user\_name  
> in session but I would like to keep only user\_id and read other  
> information from database. But I don’t want to do something like:  
> @user_name = User.find\_by\_id(session[:user\_id])  
> in every action. I would like to do that once per controller and use  
> it in all views available for these actions.  
> Is it correct and possible?

A common solution is to use a before\_filter

Fred
