# How to use a variable in several controllers?

**URL:** <https://www.ruby-forum.com/t/how-to-use-a-variable-in-several-controllers/231776>\
**Category:** Rails\
**Created:** [August 29, 2014, 9:53pm UTC](https://www.ruby-forum.com/t/how-to-use-a-variable-in-several-controllers/231776 "2014-08-29T21:53:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![addis\_a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c4cdca/32.png) [@addis\_a](https://www.ruby-forum.com/u/addis_a)\
**Post date:** [August 29, 2014, 9:53pm UTC](https://www.ruby-forum.com/t/how-to-use-a-variable-in-several-controllers/231776/1 "2014-08-29T21:53:46Z")

</div>

Dear all,

I am a new “Ruby/Ruby on Rails” programmer. I am not sure this is the  
correct forum for my question. If this is not the correct forum, please  
indicate me a more appropriate one.

When my application implements a URL, an array, @session, is used in  
several methods of a first controller. The application redirects to  
another  
page, making a second controller being invoked. This controller does not  
use the array. When the method of the second controller ends, a layout  
is  
implicitly rendered. The layout has the following code:

…

```
    <% if @session.length == 0 then %>

```

…

I got the following error message when trying to implement the URL:  
undefined  
method `length’ for nil:NilClass.

What should I do in order to the array be recognized by the second  
controller?

Regards,

António.

---

<div class="post-metadata">

**Author:** ![antonio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/6de8d8/32.png) [@antonio](https://www.ruby-forum.com/u/antonio)\
**Post date:** [August 29, 2014, 10:26pm UTC](https://www.ruby-forum.com/t/how-to-use-a-variable-in-several-controllers/231776/2 "2014-08-29T22:26:03Z")

</div>

On 29 August 2014 20:01, António [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> What should I do in order to the array be recognized by the second  
> controller?

As a newcommer to rails I strongly suggest you work right through a  
good tutorial such as [railstutorial.org](http://railstutorial.org) (which is free to use online).  
That will show you the basics of rails.

As for your question are you trying to use the rails session variable?  
If so then it is session, not @session. If it is a variable of your  
own then you cannot directly pass it from one controller to another.  
Each request to the webserver starts from a clean slate. Remember,  
for example, the server could be shut down and restart between  
requests. You must re-calculate your variable at each request. The  
exception is the session variable that I mentioned which is passed,  
normally, via cookies.

As I said work through a good tutorial before going any further.

Colin

---

<div class="post-metadata">

**Author:** ![antonio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/6de8d8/32.png) [@antonio](https://www.ruby-forum.com/u/antonio)\
**Post date:** [September 4, 2014, 10:39pm UTC](https://www.ruby-forum.com/t/how-to-use-a-variable-in-several-controllers/231776/3 "2014-09-04T22:39:24Z")

</div>

Hi Colin,

Thanks for your answer,

The variable @session is a variable I “created” to store the “user id”  
of a  
user when the user logs in.

The second controller checks the variable for knowing whether a user is  
logged in. The idea is the @session variable to be a “global variable”  
of  
the application, that is, used by several controllers. According to your  
answer, it is not possible. Is that true?

António

Sexta-feira, 29 de Agosto de 2014 21:25:00 UTC+1, Colin L. escreveu:

---

<div class="post-metadata">

**Author:** ![antonio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/6de8d8/32.png) [@antonio](https://www.ruby-forum.com/u/antonio)\
**Post date:** [September 4, 2014, 11:01pm UTC](https://www.ruby-forum.com/t/how-to-use-a-variable-in-several-controllers/231776/4 "2014-09-04T23:01:56Z")

</div>

On 4 September 2014 21:38, António [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> The second controller checks the variable for knowing whether a user is  
> logged in. The idea is the @session variable to be a “global variable” of  
> the application, that is, used by several controllers. According to your  
> answer, it is not possible. Is that true?

Whilst technically it may be possible in some situations that is  
certainly not the way to achieve the result you want. If it contains  
data that can be recalculated then possibly use a before filter.  
Alternatively the rails session variable may be the right way to go.

As I said, work right through a good tutorial and such things should  
become clear, or at least less muddy.

Colin

---

<div class="post-metadata">

**Author:** ![antonio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/6de8d8/32.png) [@antonio](https://www.ruby-forum.com/u/antonio)\
**Post date:** [September 5, 2014, 2:18pm UTC](https://www.ruby-forum.com/t/how-to-use-a-variable-in-several-controllers/231776/5 "2014-09-05T14:18:25Z")

</div>

On Friday, 29 August 2014 15:01:41 UTC-4, António wrote:

> …  
> method `length’ for nil:NilClass.
> 
> What should I do in order to the array be recognized by the second  
> controller?

Controllers and their instance variables are torn down after each  
request;  
setting @session in one won’t have any effect on subsequent requests.

You may want to look into the built-in `session` helper, which uses  
signed  
cookies:

> **[6. Session - Action Controller Overview — Ruby on Rails Guides](https://guides.rubyonrails.org/action_controller_overview.html#session)**
>
> Action Controller is the C in the Model View Controller (MVC) pattern. After the router has matched a controller to an incoming request, the controller is responsible for processing the request and generating the appropriate output. | In this guide,...

The Guides site overall will be very useful to you.

–Matt J.

---

<div class="post-metadata">

**Author:** ![antonio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/6de8d8/32.png) [@antonio](https://www.ruby-forum.com/u/antonio)\
**Post date:** [September 5, 2014, 4:16pm UTC](https://www.ruby-forum.com/t/how-to-use-a-variable-in-several-controllers/231776/6 "2014-09-05T16:16:55Z")

</div>

What you want to achieve is possible, but you are thinking about it the  
wrong way. Do not call it a “global variable” — that is not only the  
wrong word but refers to some other concept in Ruby which will not do  
what you think it will do.

Here’s a great introduction to sessions & cookies with a detailed  
beginner explanation:

[http://www.theodinproject.com/ruby-on-rails/sessions-cookies-and-authentication](http://www.theodinproject.com/ruby-on-rails/sessions-cookies-and-authentication)

when you’re in a controller and you refer to session[:xyz] (see article  
above) that is not a local variable, but in fact a special construct by  
Rails to share information across web requests from the same user. It’s  
already implemented for you – use it, don’t re-invent the wheel.

-Jason
