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
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
“Yannick M.” [email protected] wrote in
message news:[email protected]…
url_for {:action => :test}.update(test)
hth
alan
Alan B. wrote:
“Yannick M.” [email protected] wrote in
message news:[email protected]…
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 M. 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
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.
[email protected] 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
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
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,
Hi –
On Wed, 12 Jul 2006, Alan B. wrote:
“Yannick M.” [email protected] wrote in
message news:[email protected]…
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
Ruby for Rails => RUBY FOR RAILS, the Ruby book for
Rails developers
http://dablog.rubypal.com => D[avid ]A[. ]B[lack’s][ Web]log
[email protected] => me
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
Ruby for Rails => RUBY FOR RAILS (reviewed on
Slashdot, 7/12/2006!)
http://dablog.rubypal.com => D[avid ]A[. ]B[lack’s][ Web]log
[email protected] => me
plus… never trust get vars so… and whats wrong with the params
hash?
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.
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
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
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 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
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