Ruby Forum Ruby on Rails > pass hash as parameter

Posted by Yannick Majoros (Guest)
on 12.07.2006 10:31
(Received via mailing list)
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 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
Posted by Alan Bullock (Guest)
on 12.07.2006 10:37
(Received via mailing list)
"Yannick Majoros" <majoros@inma.ucl.ac.be> wrote in
message news:44B4B2C9.3050906@inma.ucl.ac.be...
> _______________________________________________


url_for {:action => :test}.update(test)

hth
alan
Posted by Yannick Majoros (Guest)
on 12.07.2006 11:30
(Received via mailing list)
Alan Bullock wrote:
> "Yannick Majoros" <majoros@inma.ucl.ac.be> wrote in 
> message news:44B4B2C9.3050906@inma.ucl.ac.be...
>   
>> _______________________________________________
>>     
>
>
> 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":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 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
Posted by Jean-François (Guest)
on 12.07.2006 11:53
(Received via mailing list)
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":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.
Posted by unknown (Guest)
on 12.07.2006 12:36
(Received via mailing list)
Hi --

On Wed, 12 Jul 2006, Alan Bullock wrote:

> "Yannick Majoros" <majoros@inma.ucl.ac.be> wrote in
> message news:44B4B2C9.3050906@inma.ucl.ac.be...
>> _______________________________________________
>
>
> 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 => Ruby/Rails training & consultancy
http://www.manning.com/black     => RUBY FOR RAILS, the Ruby book for
                                                     Rails developers
http://dablog.rubypal.com        => D[avid ]A[. ]B[lack's][ Web]log
dblack@wobblini.net              => me
Posted by Yannick Majoros (Guest)
on 13.07.2006 11:39
(Received via mailing list)
dblack@wobblini.net 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 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
Posted by Travis Michel (Guest)
on 13.07.2006 12:51
(Received via mailing list)
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, class Marshal.

cheers,
- trav
Posted by Travis Michel (Guest)
on 13.07.2006 12:55
(Received via mailing list)
plus... never trust get vars so...  and whats wrong with the params 
hash?
Posted by unknown (Guest)
on 13.07.2006 13:04
(Received via mailing list)
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 => Ruby/Rails training & consultancy
http://www.manning.com/black     => RUBY FOR RAILS (reviewed on
                                     Slashdot, 7/12/2006!)
http://dablog.rubypal.com        => D[avid ]A[. ]B[lack's][ Web]log
dblack@wobblini.net              => me
Posted by Travis Michel (Guest)
on 13.07.2006 13:17
(Received via mailing list)
> 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.
Posted by Yannick Majoros (Guest)
on 13.07.2006 13:48
(Received via mailing list)
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 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
Posted by Travis Michel (Guest)
on 13.07.2006 14:17
(Received via mailing list)
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.
Posted by Yannick Majoros (Guest)
on 13.07.2006 14:26
(Received via mailing list)
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 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