Sharing a variable between methods?

Hello!

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:


Add elements
Remove elements
Swap elements


Partial to
let user
alter the array



So, when I click on a link, it goes to, for example, an “add_element”
method and loads the partial.
Then I edit/alter/… the array, + all those things users have to do.
Then, once i am done, it goes to an “update_array” method that gets
the data from the partial and updates the array with it.

Now, the big problem i am facing :

I cannot find how to cleanly and elegantly pass the array from my
‘main_view’ method to the ‘add_element’ (or other), then to the
‘update’ method and finally back to ‘main_view’ for another round of
modifications.

I could probably pass it like this, using hidden fields or whatever
comes to my mind :

main_view method => main_view.rhtml
user clicks on add element => add_element method =>
_add_element.rhtml
user updates the array from the view and clicks on update => update
method => main_view.rhtml => …
But just thinking about how ugly it is is gonna make me have
nightmares tonight.

Or… I could make this array a sessions variable, but it’s not clean
either as it might stay in the session variable for ever… And i dont
want dust on my variables.

So, is there any nice way of doing this? Am i missing something here,
some part of Rails’ magic?
Any help would be greatly appreciated, I am kinda tired and dont want
to have nightmares tonight :slight_smile:

Thank you!

Gedeon

You could use a session variable and store it all in that.

Ryan B.
http://www.frozenplague.net
Feel free to add me to MSN and/or GTalk as this email.

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…

Gedeon

@Gedeon:

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.

Store the object in a database and then link to directly to a user? I
need
to understand what you’re trying to do first.

On Dec 27, 2007 3:37 PM, Gedeon [email protected] wrote:

Gedeon

On Dec 27, 11:19 am, “Ryan B.” [email protected] wrote:

You could use a session variable and store it all in that.

Ryan B.http://www.frozenplague.net
Feel free to add me to MSN and/or GTalk as this email.


Ryan B.
http://www.frozenplague.net
Feel free to add me to MSN and/or GTalk as this email.

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,

Gedeon

On Dec 26, 2007, at 11:26 PM, Gedeon wrote:

I guess I’ll do that then :slight_smile:
I am already using the session variable but only for ‘small’ things,

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.

Hello

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.

Gilles.

If the list they are creating is important enough that it should be
stored in the database and displayed as a list to the user using
something such as the “acts_as_list” plugin. If the list is not database
worthy, then store it in the session.

In Rails 2.0, session data is actually stored as an encrypted cookie on
the client computer by default. All the data is stored on the client
computer and as such there is no “cruft” to build up. Not to mention,
you can clear the session data at any time once they are finished.

There is really only five ways to persist data between actions (read:
requests) :

  1. Session
  2. Cookies
  3. Database
  4. Files
  5. Background Process

For your needs, it seems that persisting an array would be best suited
in the session or in the database.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs