# Pass content of Array to helper

**URL:** <https://www.ruby-forum.com/t/pass-content-of-array-to-helper/134200>\
**Category:** Rails\
**Created:** [April 8, 2008, 8:43pm UTC](https://www.ruby-forum.com/t/pass-content-of-array-to-helper/134200 "2008-04-08T20:43:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![lasastard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/58956e/32.png) [@lasastard](https://www.ruby-forum.com/u/lasastard)\
**Post date:** [April 8, 2008, 8:43pm UTC](https://www.ruby-forum.com/t/pass-content-of-array-to-helper/134200/1 "2008-04-08T20:43:40Z")

</div>

Hi,

Ive been trying to to do the following and cant figure out how…

On my page some output is generated within a helper - an array. The  
content of the array is listed on the page, but I want to be able to  
click on a button at the bottom of the page to take the array and  
transfer it’s content to another method and view, instead of having to  
re-generate it from scratch. Now, both link\_to and button\_to do not seem  
to accept an array as option, just integers and strings (like “link\_to  
‘next\_page’, :action =\> ‘some\_method’, :array =\> @some\_array”).  
And else, how to I process the array in the new method. SO far I only  
now the params[:array] option - is there perhaps anything else? Like  
@array = :array…

Any advice on how to do this in practice would be greatly appreciated…  
maybe I am just being too tired now, but I really cant figure it out :\_.

Cheers,

Marc

---

<div class="post-metadata">

**Author:** ![lasastard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/58956e/32.png) [@lasastard](https://www.ruby-forum.com/u/lasastard)\
**Post date:** [April 9, 2008, 7:01am UTC](https://www.ruby-forum.com/t/pass-content-of-array-to-helper/134200/2 "2008-04-09T07:01:28Z")

</div>

Its ugly and the array could potentially blow out the url, but you could  
always translate the array into a big-a$$ string parameter and pass it  
back that way.

Gives me the willies to think about it though…

---

<div class="post-metadata">

**Author:** ![lasastard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/58956e/32.png) [@lasastard](https://www.ruby-forum.com/u/lasastard)\
**Post date:** [April 10, 2008, 11:07pm UTC](https://www.ruby-forum.com/t/pass-content-of-array-to-helper/134200/3 "2008-04-10T23:07:57Z")

</div>

Marc,

You could do Ar’s idea. Since the array is generated by a helper, it’s  
on the view and you need to plug it in somehow so that the view then  
needs to pass it back in a new request to the controller. You could  
also try passing it as a JSON object.

Another thought: can you generate the array in the controller before  
you call the view? If so, then you could save the array as a  
serialized value on the session. Or, serialize it to disk.

I guess it comes down to when the array is created. If it’s only in  
the view… like some javascript that creates it or something… then  
you probably will need to store it in some variable so it can be  
passed to the controller… which means Ar’s idea (or JSON or some  
similar format).

Tell us a little more what you are trying to do and maybe there’s a  
better way overall.

Usually, though, if you need to move data back and forth between  
requests, you use the session if the data isn’t too big, or the  
database, or the filesystem (i.e. dump it to XML then read it back in,  
or something like that).

Just some thoughts…

-Danimal

---

<div class="post-metadata">

**Author:** ![lasastard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/58956e/32.png) [@lasastard](https://www.ruby-forum.com/u/lasastard)\
**Post date:** [April 10, 2008, 11:27pm UTC](https://www.ruby-forum.com/t/pass-content-of-array-to-helper/134200/4 "2008-04-10T23:27:47Z")

</div>

Look at Marshal#dump / Marshal#load

$ irb

> > a = %w{ one two three}  
> > =\> [“one”, “two”, “three”]
> 
> > dumped = Marshal.dump(a)  
> > =\> “\004\b[\b”\bone"\btwo"\nthree"
> 
> > Marshal.load(dumped)  
> > =\> [“one”, “two”, “three”]

Then need to “url-escape”:

> > require ‘cgi’  
> > =\> true
> 
> > dumped\_urlencoded = CGI.escape(dumped)  
> > =\> “%04%08%5B%08%22%08one%22%08two%22%0Athree”
> 
> > CGI.unescape(dumped\_urlencoded)  
> > =\> “\004\b[\b”\bone"\btwo"\nthree"
> 
> > Marshal.load(CGI.unescape(dumped\_urlencoded))  
> > =\> [“one”, “two”, “three”]

Stephan
