Forum: Ruby on Rails How to Pass a Large Array from one Action to Another

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Joe P. (Guest)
on 2008-10-30 17:19
Good morning everybody, how are you?

Okay, this should be easy.  I am generating a large array in one action,
and I want to pass this array to another action when the user pushes a
button.

@problems is a large array (about 2,000 items) that I need to pass to
the action "buy_cards".  What is the best way to do this?  I'm pretty
sure it's too big for a session, and I don't want to store it in DB,
since I just need it temporarily:

<%= button_to "Buy Cars", :action => :buy_cars, :car_id => @file_id,
:problems => @problems %>

Anyone know a good way to pass a large array from one action to another?
Robert W. (Guest)
on 2008-10-30 17:43
How about putting a serialized version of it as an attachment using a
multipart-form? As long as you're using a POST in your form action there
shouldn't be any set limit on the size of data that you're posting.

@problems.to_yaml (or @problems.to_json) should give you a textual
format. Then in the receiving action de-serialize the yaml (or json).

Joe P. wrote:
> Good morning everybody, how are you?
>
> Okay, this should be easy.  I am generating a large array in one action,
> and I want to pass this array to another action when the user pushes a
> button.
>
> @problems is a large array (about 2,000 items) that I need to pass to
> the action "buy_cards".  What is the best way to do this?  I'm pretty
> sure it's too big for a session, and I don't want to store it in DB,
> since I just need it temporarily:
>
> <%= button_to "Buy Cars", :action => :buy_cars, :car_id => @file_id,
> :problems => @problems %>
>
> Anyone know a good way to pass a large array from one action to another?
Jeff P. (Guest)
on 2008-10-31 03:47
Joe P. wrote:
> Good morning everybody, how are you?
>
> Okay, this should be easy.  I am generating a large array in one action,
> and I want to pass this array to another action when the user pushes a
> button.
>
> Anyone know a good way to pass a large array from one action to another?

I would recommend serializing it into a temp file on the server and
reading it back out in the other action. You should be able to serialize
it with "to_json", and just write it to a file.  Then read it back into
an array in the other action.  Name the temp file relative to the user
so you don't have to worry about a name collision, and then pass the
path to the temp file in the session.

HTH,
jp
This topic is locked and can not be replied to.