Information in all controllers?

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

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

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

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.

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