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 ---------------------------------------------------------------------- Mon calendrier en ligne : http://www.inma.ucl.ac.be/~majoros/calendar Accents bizarres ? http://www.inma.ucl.ac.be/~majoros/email.html ---------------------------------------------------------------------- -------------- next part -------------- A non-text attachment was scrubbed... Name: smime.p7s Type: application/x-pkcs7-signature Size: 3403 bytes Desc: S/MIME Cryptographic Signature Url : http://wrath.rubyonrails.org/pipermail/rails/attachments/20060712/2cebcbf4/smime-0001.bin
"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
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 ---------------------------------------------------------------------- Mon calendrier en ligne : http://www.inma.ucl.ac.be/~majoros/calendar Accents bizarres ? http://www.inma.ucl.ac.be/~majoros/email.html ---------------------------------------------------------------------- -------------- next part -------------- A non-text attachment was scrubbed... Name: smime.p7s Type: application/x-pkcs7-signature Size: 3403 bytes Desc: S/MIME Cryptographic Signature Url : http://wrath.rubyonrails.org/pipermail/rails/attachments/20060712/1b6ab582/smime.bin
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. -- ? la renverse.
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
dblack@wobblini.net wrote:> 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 >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 ---------------------------------------------------------------------- Mon calendrier en ligne : http://www.inma.ucl.ac.be/~majoros/calendar Accents bizarres ? http://www.inma.ucl.ac.be/~majoros/email.html ---------------------------------------------------------------------- -------------- next part -------------- A non-text attachment was scrubbed... Name: smime.p7s Type: application/x-pkcs7-signature Size: 3403 bytes Desc: S/MIME Cryptographic Signature Url : http://wrath.rubyonrails.org/pipermail/rails/attachments/20060713/1d360ee5/smime.bin
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 -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060713/cc3ccf6a/attachment.html
plus... never trust get vars so... and whats wrong with the params hash? -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060713/7f78dbe8/attachment.html
Hi -- On Thu, 13 Jul 2006, Travis Michel wrote:> Its not a part of rails, but you could try Marshal #dump and #loadMarshal 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
> Marshal is no less a part of Rails than Class, Time, YAML, CGI, and > Fixnum are. So dig in :-) > > > DavidThis 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. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060713/d2ac0af3/attachment.html
Travis Michel wrote:> > 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.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 ---------------------------------------------------------------------- Mon calendrier en ligne : http://www.inma.ucl.ac.be/~majoros/calendar Accents bizarres ? http://www.inma.ucl.ac.be/~majoros/email.html ---------------------------------------------------------------------- -------------- next part -------------- A non-text attachment was scrubbed... Name: smime.p7s Type: application/x-pkcs7-signature Size: 3403 bytes Desc: S/MIME Cryptographic Signature Url : http://wrath.rubyonrails.org/pipermail/rails/attachments/20060713/98f45996/smime.bin
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. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060713/11067334/attachment.html
Travis Michel wrote:> 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 >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 ---------------------------------------------------------------------- Mon calendrier en ligne : http://www.inma.ucl.ac.be/~majoros/calendar Accents bizarres ? http://www.inma.ucl.ac.be/~majoros/email.html ---------------------------------------------------------------------- -------------- next part -------------- A non-text attachment was scrubbed... Name: smime.p7s Type: application/x-pkcs7-signature Size: 3403 bytes Desc: S/MIME Cryptographic Signature Url : http://wrath.rubyonrails.org/pipermail/rails/attachments/20060713/276382d6/smime-0001.bin