Matthew Westcott
2007-Jul-10 23:01 UTC
Using arbitrary strings for keys in the params hash
Hi,
Is there any way to construct a GET or POST request so that the
resulting params hash can incorporate arbitrary strings (i.e.
including ampersands, quotes, square brackets...) as its keys? I''m
trying to do something like
<form action="/translations/create" form="post">
<input type="text" name="translations[Hello]"
value="Bonjour" />
<input type="text" name="translations[Press [OK] to
continue]"
value="Touchez [OK] pour continuer" />
<input type="submit" />
</form>
to get back a params hash like
{"translations"=>{
"Hello"=>"Bonjour",
"Press [OK] to continue"=>"Touchez [OK] pour continuer"
}}
but the square brackets in the second field name cause it to be
omitted from the hash. I''ve tried escaping them with slashes, URL-
encoding and everything else I can think of, but it makes no
difference.
I guess one workaround would be to roll my own encoding scheme
(something like URL encoding but not quite...) and explicitly decode
the names at the other end, but it would be nice to be able to avoid
that step. Any suggestions?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Matthew Westcott
2007-Jul-11 08:24 UTC
Re: Using arbitrary strings for keys in the params hash
On Jul 11, 1:01 am, Matthew Westcott <matt...-EVGsuJHbCVyNPqACXyvcFA@public.gmane.org> wrote:> I''m trying to do something like > > <form action="/translations/create" form="post"> > <input type="text" name="translations[Hello]" value="Bonjour" /> > <input type="text" name="translations[Press [OK] to continue]" > value="Touchez [OK] pour continuer" /> > <input type="submit" /> > </form>Never mind... it occurred to me shortly after posting that I was being needlessly ''cute'' with the params hash there, and would be far better off refactoring slightly: <form action="/translations/create" form="post"> <input type="hidden" name="translations[0][original]" value="Hello" /> <input type="text" name="translations[0][translation]" value="Bonjour" /> <input type="hidden" name="translations[1][original]" value="Press [OK] to continue" /> <input type="text" name="translations[1][translation]" value="Touchez [OK] pour continuer" /> <input type="submit" /> </form> along with @translations = {} for entry in params[:translations] @translations[entry[''original'']] = entry[''translation''] end --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---