Forum: Ruby on Rails Information in all controllers?

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
4bb313af528d968c3a35e8208ecd287b?d=identicon&s=25 Shagymoe (Guest)
on 2007-01-12 14:53
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.


Ad09c60ee5513634c1f160ffa48e4325?d=identicon&s=25 mayoz (Guest)
on 2007-01-12 18:01
I would say save the cart object in the session and you can get the
information back in every controller you are using.
5c8d28d1333f8e44641e74c9f72c29c7?d=identicon&s=25 Snow Man (snowman)
on 2007-01-13 18:58
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
D0cd6b10e01bacb976b3b815a9c660bc?d=identicon&s=25 Alex Wayne (squeegy)
on 2007-01-14 09:47
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.
8b22ad683842861ba91f68d5d32954db?d=identicon&s=25 Jason Norris (Guest)
on 2007-01-19 16:29
(Received via mailing list)
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)

This topic is locked and can not be replied to.