Pass content of Array to helper

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

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…

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

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

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