Sharing a variable between methods ?


I need some help on something that seems very basic and 'noobish' to me but that I cant figure out... I'm going to try to make it as simple as possible...

I have a method that initializes an array. Let's call it "main_view'. Why? Because it is the main view. The corresponding view - main_view.rhtml - does not do anything with this array, instead, it contains links to ajax-powered partials that let users alter this array in different ways, depending on what link they click on. If my page was made in ascii-'art' and I had a 2 minutes deadline, it would probably look as bad as this:

Thank you Ryan, Yes, I could do that but from what i read, it is not recommended because of unexpired sessions that will pile up and could create a performance problem... Which is why I am posting here.

It should be ok for my project as it will have a limited number of users, but still, I'd rather like to make it clean and scalable... Besides, I'm sure I'll bump into this problem again on other projects so I'll need a better solution...



For something like this, I would definitely use the session. The purpose of the session is to persist data between requests. Do not worry about unexpired sessions… unless you have explicitly turned sessions OFF in your controllers, you’re already creating them. Investigate database session storage or cookie-based storage as they each have benefits and disadvantages.

I guess I'll do that then :slight_smile: I am already using the session variable but only for 'small' things, such as the currently logged in user id, selected css id, ... For some reason, I was scared i might cause some kind of meltdown, lose my hair,... or even worse by storing 'bigger' variables in there.

Thank you,


I was doing that for a while, but with large arrays things would get clobbered and I'd get a 500 error. What I did instead is put enough info in the the session to know how to rebuild the array.


You could investigate nondatabase Model too ... this model object could act as a wrapper for your array and persist between request (you may or may not store it in the session)

Don't really know if it would fit, you'll have to investigate a bit.