# Information in all controllers?

**URL:** <https://www.ruby-forum.com/t/information-in-all-controllers/81743>\
**Category:** Rails\
**Created:** [January 12, 2007, 2:53pm UTC](https://www.ruby-forum.com/t/information-in-all-controllers/81743 "2007-01-12T14:53:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![shagymoe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ebca7d/32.png) [@shagymoe](https://www.ruby-forum.com/u/shagymoe)\
**Post date:** [January 12, 2007, 2:53pm UTC](https://www.ruby-forum.com/t/information-in-all-controllers/81743/1 "2007-01-12T14:53:23Z")

</div>

Basically, I have a store controller and an account controller (using  
acts\_as\_authenticated). In the store controller, I have a before\_filter  
which populates some instance variables with information about the cart  
to be used in most of the store views. So, imagine that a “guest” user  
is shopping and adding things to the cart. One of the side boxes  
displays the cart content as well as some other information about his  
cart using the information populated by the before\_filter. When he  
wants to check out, he has to log in/create an account. As soon as the  
site goes to the account controller to log him in, he loses the  
information that was being generated by the before\_filter in the store  
controller (of course) and the view fails.

So, my question is, what is the best way to design this? Make the  
information available to all controllers? (if so, how?) The account  
controller’s information is available to all controllers. Is this  
because it is a plug-in or the way it is designed? Maybe I don’t  
totally grock the controller thing because the impatient part of me is  
about ready to throw everything in one controller just so it’s available  
everywhere. LOL

Any advice is greatly appreciated.

Thanks,

Shagy

---

<div class="post-metadata">

**Author:** ![shagymoe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ebca7d/32.png) [@shagymoe](https://www.ruby-forum.com/u/shagymoe)\
**Post date:** [January 12, 2007, 6:01pm UTC](https://www.ruby-forum.com/t/information-in-all-controllers/81743/2 "2007-01-12T18:01:46Z")

</div>

I would say save the cart object in the session and you can get the  
information back in every controller you are using.

---

<div class="post-metadata">

**Author:** ![shagymoe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ebca7d/32.png) [@shagymoe](https://www.ruby-forum.com/u/shagymoe)\
**Post date:** [January 13, 2007, 6:58pm UTC](https://www.ruby-forum.com/t/information-in-all-controllers/81743/3 "2007-01-13T18:58:18Z")

</div>

Just to extend a bit on what mayoz said…

Any data that you want to save from one request to the next you MUST put  
in either the database or the session. You MUST NEVER try to save data  
in the controller or any other in-process location. Remember that when  
you deploy your application you will almost certainly have multiple  
instances of your application running, each with its own independent  
state, and there is no way to guarantee that a particular user will be  
connected to the same instance on each request.

To save data in the session, in your controller just do:

session[:some\_identifier] = some\_data

Then to get it back during the next request,

some\_data = session[:some\_identifier]

Each user has his or her own private session that nobody else can see,  
so you don’t need to explicitly link the data to a particular user. In  
your case, you probably just want to do:

session[:cart] = object\_containing\_cart\_information

---

<div class="post-metadata">

**Author:** ![shagymoe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ebca7d/32.png) [@shagymoe](https://www.ruby-forum.com/u/shagymoe)\
**Post date:** [January 14, 2007, 9:47am UTC](https://www.ruby-forum.com/t/information-in-all-controllers/81743/4 "2007-01-14T09:47:38Z")

</div>

mayoz wrote:

> I would say save the cart object in the session and you can get the  
> information back in every controller you are using.

Do this, and add a before\_filter to application.rb. Any instance  
varibales you set via before filter in application.rb will be available  
in all controller actions, for all controllers.

---

<div class="post-metadata">

**Author:** ![shagymoe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ebca7d/32.png) [@shagymoe](https://www.ruby-forum.com/u/shagymoe)\
**Post date:** [January 19, 2007, 4:29pm UTC](https://www.ruby-forum.com/t/information-in-all-controllers/81743/5 "2007-01-19T16:29:18Z")

</div>

If the cart is stored in the session, it should persist from being a  
guest to when they’re logged in. Maybe it’s your plug-in that’s clearing  
your session out. I’ve always found it easier to write my own code than  
use a plugin, precisely for reasons like this. If you wrote the code,  
you’re sure to know what it’s doing and how to change it. If someone  
else wrote it, you’re going to have to spend time learning how they  
wrote it and you might as well have written your own code by the time  
it’s said and done. (IMO)

Jason
