# Pass hash as parameter

**URL:** <https://www.ruby-forum.com/t/pass-hash-as-parameter/98055>\
**Category:** Rails\
**Created:** [July 12, 2006, 10:31am UTC](https://www.ruby-forum.com/t/pass-hash-as-parameter/98055 "2006-07-12T10:31:09Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![ymartin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/779978/32.png) [@ymartin](https://www.ruby-forum.com/u/ymartin)\
**Post date:** [July 12, 2006, 10:31am UTC](https://www.ruby-forum.com/t/pass-hash-as-parameter/98055/1 "2006-07-12T10:31:09Z")

</div>

Hello,

How can I pass a hash as parameter from a template with url\_for?

e.g. : if I have

test = { :a =\> “a”, :b =\> “b” }

How can I pass that with my request? Is it possible to pass nested  
hash’es to?

Right now,

url\_for :action =\> :test, test

doesn’t seem to work.

Thank you,

## –

Yannick M. [http://www.inma.ucl.ac.be/~majoros](http://www.inma.ucl.ac.be/~majoros)  
Informaticien UCL/INMA-MEMA  
4, avenue G. LemaÃ®tre  
B-1348 Louvain-la-Neuve  
Tel: +32-10-47.80.10  
Fax: +32-10-47.21.80

---

<div class="post-metadata">

**Author:** ![ymartin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/779978/32.png) [@ymartin](https://www.ruby-forum.com/u/ymartin)\
**Post date:** [July 12, 2006, 10:37am UTC](https://www.ruby-forum.com/t/pass-hash-as-parameter/98055/2 "2006-07-12T10:37:38Z")

</div>

“Yannick M.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote in  
message news:removed\_email\_address@domain.invalid…

> * * *

url\_for {:action =\> :test}.update(test)

hth  
alan

---

<div class="post-metadata">

**Author:** ![ymartin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/779978/32.png) [@ymartin](https://www.ruby-forum.com/u/ymartin)\
**Post date:** [July 12, 2006, 11:30am UTC](https://www.ruby-forum.com/t/pass-hash-as-parameter/98055/3 "2006-07-12T11:30:47Z")

</div>

Alan B. wrote:

> “Yannick M.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote in  
> message news:removed\_email\_address@domain.invalid…
> 
> > * * *
> 
> url\_for {:action =\> :test}.update(test)

This looks strange to me, and I can’t make it work: url\_for returns a  
string, and I get "undefined method `update’ for  
“[http://myserver:3000/x/test](http://myserver:3000/x/test)”:String ". I had to change the syntax for  
url\_for, too. And actually, I am not sure about “update”, what should  
that do?

Thanks anyway…

## –

Yannick M. [http://www.inma.ucl.ac.be/~majoros](http://www.inma.ucl.ac.be/~majoros)  
Informaticien UCL/INMA-MEMA  
4, avenue G. LemaÃ®tre  
B-1348 Louvain-la-Neuve  
Tel: +32-10-47.80.10  
Fax: +32-10-47.21.80

---

<div class="post-metadata">

**Author:** ![ymartin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/779978/32.png) [@ymartin](https://www.ruby-forum.com/u/ymartin)\
**Post date:** [July 12, 2006, 11:53am UTC](https://www.ruby-forum.com/t/pass-hash-as-parameter/98055/4 "2006-07-12T11:53:37Z")

</div>

Hello Yannick,

> > url\_for {:action =\> :test}.update(test)
> 
> This looks strange to me, and I can’t make it work:  
> url\_for returns a string, and I get "undefined method `update’  
> for “[http://myserver:3000/x/test](http://myserver:3000/x/test)”:String ". I had to change  
> the syntax for url\_for, too. And actually, I am not sure about  
> “update”, what should that do?

You can put parenthesis or use a local variable :

url\_for( { :action =\> :test}.update(test) )

or

my\_hash = { :action =\> :test}.update(test)  
url\_for my\_hash

Hash#update merges 2 hash objects together. ( alias for  
Hash#merge! )

– Jean-FranÃ§ois.

---

<div class="post-metadata">

**Author:** ![ymartin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/779978/32.png) [@ymartin](https://www.ruby-forum.com/u/ymartin)\
**Post date:** [July 13, 2006, 11:39am UTC](https://www.ruby-forum.com/t/pass-hash-as-parameter/98055/5 "2006-07-13T11:39:22Z")

</div>

removed\_email\_address@domain.invalid wrote:

> Actually that will try to parse the hash as a code block. You need  
> parentheses around the hash.
> 
> David

Hi again,

Ok, my real problem wasn’t about the syntax. The problem is that it  
seems that url\_for doesn’t treat hashes really well:

url\_for :test =\> { :dummy =\> 1, :pilou =\> 5 }

returns this string:

/mycontroller/list?test=pilou5dummy1

What I’d like to have is this:

/mycontroller/list?test[pilou]=5&test[dummy]=1

(and I’d even like to have more deeply nested hashes)

So, is there anything for that in rails?

Kind regards,

## –

Yannick M. [http://www.inma.ucl.ac.be/~majoros](http://www.inma.ucl.ac.be/~majoros)  
Informaticien UCL/INMA-MEMA  
4, avenue G. LemaÃ®tre  
B-1348 Louvain-la-Neuve  
Tel: +32-10-47.80.10  
Fax: +32-10-47.21.80

---

<div class="post-metadata">

**Author:** ![ymartin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/779978/32.png) [@ymartin](https://www.ruby-forum.com/u/ymartin)\
**Post date:** [July 13, 2006, 12:51pm UTC](https://www.ruby-forum.com/t/pass-hash-as-parameter/98055/6 "2006-07-13T12:51:54Z")

</div>

Its not a part of rails, but you could try Marshal #dump and #load

example:

url\_for :action =\> ‘list’, :params =\> {:data =\> Marshal.dump(HASH)}

and on the receiving side do:

data = Marshal.load(@params[‘data’])

Marshal is fairly safe, for stipulations check out the core docs on  
[ruby-doc.org](http://ruby-doc.org), class Marshal.

cheers,

- trav

---

<div class="post-metadata">

**Author:** ![ymartin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/779978/32.png) [@ymartin](https://www.ruby-forum.com/u/ymartin)\
**Post date:** [July 12, 2006, 12:36pm UTC](https://www.ruby-forum.com/t/pass-hash-as-parameter/98055/7 "2006-07-12T12:36:52Z")

</div>

Hi –

On Wed, 12 Jul 2006, Alan B. wrote:

> “Yannick M.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote in  
> message news:removed\_email\_address@domain.invalid…
> 
> > * * *
> 
> url\_for {:action =\> :test}.update(test)

Actually that will try to parse the hash as a code block. You need  
parentheses around the hash.

David

–  
[http://www.rubypowerandlight.com](http://www.rubypowerandlight.com) =\> Ruby/Rails training & consultancy  
[Ruby for Rails](http://www.manning.com/black) =\> RUBY FOR RAILS, the Ruby book for  
Rails developers  
[http://dablog.rubypal.com](http://dablog.rubypal.com) =\> D[avid]A[.]B[lack’s][Web]log  
removed\_email\_address@domain.invalid =\> me

---

<div class="post-metadata">

**Author:** ![ymartin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/779978/32.png) [@ymartin](https://www.ruby-forum.com/u/ymartin)\
**Post date:** [July 13, 2006, 1:04pm UTC](https://www.ruby-forum.com/t/pass-hash-as-parameter/98055/8 "2006-07-13T13:04:49Z")

</div>

Hi –

On Thu, 13 Jul 2006, Travis Michel wrote:

> Its not a part of rails, but you could try Marshal #dump and #load

Marshal is no less a part of Rails than Class, Time, YAML, CGI, and  
Fixnum are. So dig in 🙂

David

–  
[http://www.rubypowerandlight.com](http://www.rubypowerandlight.com) =\> Ruby/Rails training & consultancy  
[Ruby for Rails](http://www.manning.com/black) =\> RUBY FOR RAILS (reviewed on  
Slashdot, 7/12/2006!)  
[http://dablog.rubypal.com](http://dablog.rubypal.com) =\> D[avid]A[.]B[lack’s][Web]log  
removed\_email\_address@domain.invalid =\> me

---

<div class="post-metadata">

**Author:** ![ymartin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/779978/32.png) [@ymartin](https://www.ruby-forum.com/u/ymartin)\
**Post date:** [July 13, 2006, 12:55pm UTC](https://www.ruby-forum.com/t/pass-hash-as-parameter/98055/9 "2006-07-13T12:55:13Z")

</div>

plus… never trust get vars so… and whats wrong with the params  
hash?

---

<div class="post-metadata">

**Author:** ![ymartin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/779978/32.png) [@ymartin](https://www.ruby-forum.com/u/ymartin)\
**Post date:** [July 13, 2006, 1:17pm UTC](https://www.ruby-forum.com/t/pass-hash-as-parameter/98055/10 "2006-07-13T13:17:28Z")

</div>

> Marshal is no less a part of Rails than Class, Time, YAML, CGI, and  
> Fixnum are. So dig in 🙂
> 
> David

This is true. I can’t imagine how Marshal can simplify get params  
though.  
I’d like to hear why Yannick is uncomfortable with the standard params  
notation. Remember, “Constraints are liberating”. As long as we  
understand  
them of course.

---

<div class="post-metadata">

**Author:** ![ymartin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/779978/32.png) [@ymartin](https://www.ruby-forum.com/u/ymartin)\
**Post date:** [July 13, 2006, 1:48pm UTC](https://www.ruby-forum.com/t/pass-hash-as-parameter/98055/11 "2006-07-13T13:48:13Z")

</div>

Travis Michel wrote:

> standard params notation. Remember, “Constraints are liberating”. As  
> long as we understand them of course.

Either I don’t understand them, either theses specific constraints  
aren’t that liberating 😉 Could it be that it something that should be  
implemented but never was.

I’d just like to pass a hash of hashes of hashes to my controller, for  
example. And I don’t want to rewrite each parameter like “x[y]=5&x[z]=3”  
by hand when I have x =\> { :y =\>5, :z=3 }, my opinion is that just  
passing x to url\_for should be enough.

And so, I don’t have anything agains the param array. I just don’t know  
why url\_for is less “magic” than the code which initializes params and  
know what it should to with hashes :-/

What do you think?

## –

Yannick M. [http://www.inma.ucl.ac.be/~majoros](http://www.inma.ucl.ac.be/~majoros)  
Informaticien UCL/INMA-MEMA  
4, avenue G. LemaÃ®tre  
B-1348 Louvain-la-Neuve  
Tel: +32-10-47.80.10  
Fax: +32-10-47.21.80

---

<div class="post-metadata">

**Author:** ![ymartin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/779978/32.png) [@ymartin](https://www.ruby-forum.com/u/ymartin)\
**Post date:** [July 13, 2006, 2:17pm UTC](https://www.ruby-forum.com/t/pass-hash-as-parameter/98055/12 "2006-07-13T14:17:13Z")

</div>

Honestly, I don’t see why not. Personally, I feel that the current  
params  
is sufficient because I don’t mind prebuilding any structure I need in  
the  
controllers. However, if you want to use a premade hash rather than  
building one, I can see some coding shortcuts.

This might help…

Add to a helper:

def to\_url( name, hash )  
outs = ‘’  
hash.each\_pair { |key,value| outs +=  
name+’[’+key.to\_s+’]=’+value.to\_s+’&’  
}  
outs.chomp(’&’)  
end

Although its not pretty to use in views (still handwritten) I suppose it  
might help you out a bit. This might be something to add to the current  
url  
helper… but I can’t help you out there.

I can imagine something like:  
url\_for( :action =\> ‘aoeu’, :append =\> { :test =\> HASH } )

much like the :anchor key.

good luck,

- travis.

---

<div class="post-metadata">

**Author:** ![ymartin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/779978/32.png) [@ymartin](https://www.ruby-forum.com/u/ymartin)\
**Post date:** [July 13, 2006, 2:26pm UTC](https://www.ruby-forum.com/t/pass-hash-as-parameter/98055/13 "2006-07-13T14:26:28Z")

</div>

Travis Michel wrote:

> outs = ‘’  
> hash.each\_pair { |key,value| outs +=  
> name+‘[’+key.to\_s+‘]=’+value.to\_s+‘&’ }  
> outs.chomp(‘&’)  
> end

Seems good, I’ll implement it as soon as I can. I’ll just add some  
recursivity for hashes of hashes… 🙂

> Although its not pretty to use in views (still handwritten) I suppose  
> it might help you out a bit. This might be something to add to the  
> current url helper… but I can’t help you out there.

Would be great, I think 😉

> I can imagine something like:  
> url\_for( :action =\> ‘aoeu’, :append =\> { :test =\> HASH } )
> 
> much like the :anchor key.

Thank you very much,

## –

Yannick M. [http://www.inma.ucl.ac.be/~majoros](http://www.inma.ucl.ac.be/~majoros)  
Informaticien UCL/INMA-MEMA  
4, avenue G. LemaÃ®tre  
B-1348 Louvain-la-Neuve  
Tel: +32-10-47.80.10  
Fax: +32-10-47.21.80
