Problem with toQueryParams()

11 views
Skip to first unread message

mr_justin

unread,
Apr 17, 2007, 6:31:48 PM4/17/07
to Ruby on Rails: Spinoffs
I'm sure this has been covered before, but here goes...

Anybody noticing some odd behavior with submitting forms through
Ajax.Request? I thought at first it was a problem with Form.serialize,
but was able to trace it back to line #975 (in prototype 1.5.1_rc2)
which calls "toQueryParams" on the parameters that are passed into the
new Ajax object if it is a string. This is the code (line #975 is the
second line):

if (typeof this.options.parameters == 'string')
this.options.parameters = this.options.parameters.toQueryParams();

A quick example could be seen by running the following in Firebug:
"foo=bar&apple=&orange=".toQueryParams()

The apple and orange parameters are left off due to being empty. In
terms of forms and empty fields (which should trigger validation
errors), this seems like a huge problem.

Is my version of prototype just out-of-date or is there a fix for this
in the foreseeable future? I know I can extend/override to fix this
behavior. I'm not looking for a fix, just wondering if anybody else
has seen this.

-justin

mr_justin

unread,
Apr 18, 2007, 11:19:14 AM4/18/07
to Ruby on Rails: Spinoffs
Nobody has run into this? It is impossible to submit empty form fields
through Ajax with this new behavior in Prototype. The best solution is
to just switch forms to use normal POSTs, that's lame.

You can reproduce by doing my previous example, or even:

new Ajax.Request('/', { parameters: 'x=&y=&foo=bar' } )

Take a look at what was POSTed, it's not what you'd expect.

-justin

Michael Peters

unread,
Apr 18, 2007, 12:06:11 PM4/18/07
to rubyonrail...@googlegroups.com
mr_justin wrote:
> Nobody has run into this? It is impossible to submit empty form fields
> through Ajax with this new behavior in Prototype.

Sorry no-one has responded yet. It's been talked about quite a bit.

http://groups.google.com/group/prototype-core/browse_thread/thread/711012b806b3ad36/75b3de9077ec6007#75b3de9077ec6007
http://groups.google.com/group/prototype-core/browse_thread/thread/83996a20fecd61c4/8775c0b24361ae60#8775c0b24361ae60

It has been fixed in SVN and is scheduled to be in 1.5.1

> The best solution is
> to just switch forms to use normal POSTs, that's lame.

You might try passing parameters as an object instead of a string so that
toQueryParams() is not called.

new Ajax.Request('/', { parameters: { x: '', y: '', foo: 'bar' } } )

--
Michael Peters
Developer
Plus Three, LP

Justin Perkins

unread,
Apr 18, 2007, 12:17:05 PM4/18/07
to rubyonrail...@googlegroups.com
Thanks for your response Michael. I guess I should have clarified that
I'm using Rails built-in helpers like form_remote_tag and
link_to_remote, which generate the JavaScript for us. Extending either
JS or Rails seems like an adequate fix, for now.

-justin

tobie

unread,
Apr 18, 2007, 12:24:44 PM4/18/07
to Ruby on Rails: Spinoffs
Hi Justin,

This has been corrected in the svn trunk:

$H("foo=bar&apple=&orange=".toQueryParams()).toQueryString()
// => "foo=bar&apple=&orange="

Regards,

Tobie

Justin Perkins

unread,
Apr 18, 2007, 12:32:52 PM4/18/07
to rubyonrail...@googlegroups.com
Indeed it is, thanks Tobie!

-justin

Reply all
Reply to author
Forward
0 new messages