Hello, I''d like to send a javascript object to the server using Ajax.Request and have the parameters show up as entries in the params hash. My javascript object contains other objects (e.g. prototype range objects) and I''d like to maintain the same structure in my params hash in my action function. Initially I tried somthing like this: var searchParams = { a: 10, b: "hello", c: $R(0,40) } var stuff = new Ajax.Request(''/browse/get_stuff'', { method: ''get'', parameters: Object.toQueryString(searchParams) }); This almost works perfectly, but the range turns into "[Object object]" and params.inspect returns this: {"a"=>10, "b"=>"hello", "c"=>"[object Object]", "action"=>"get_stuff", "controller"=>"browse"} After reading a bit, I''ve come to understand that for nested objects the preferred method is Object.toJSON rather than Object.toQueryString. So I changed my code to this: var searchParams = { a: 10, b: "hello", c: $R(0,40) } var stuff = new Ajax.Request(''/browse/get_stuff'', { method: ''get'', parameters: Object.toJSON(searchParams) }); But parameters turns into a single string, rather than individual entries in params, and params.inspect returns this: {"action"=>"get_stuff", "{\"a\": 10, \"b\": \"hello\", \"c\": {\"start\": 0, \"end\": 40}}"=>nil, "controller"=>"browse"} How can I get my object hash into Ruby as a hash? Thanks, Mike. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Unsure, but try this ... var searchParams = { a: 10, b: "hello", c: Object.toJSON($R(0,40)) }; On 06/02/2008, Mike Pelley <mikepelley-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Hello, > > I''d like to send a javascript object to the server using Ajax.Request > and have the parameters show up as entries in the params hash. My > javascript object contains other objects (e.g. prototype range > objects) and I''d like to maintain the same structure in my params hash > in my action function. > > Initially I tried somthing like this: > > var searchParams = { a: 10, b: "hello", c: $R(0,40) } > > var stuff = new Ajax.Request(''/browse/get_stuff'', { > method: ''get'', > parameters: Object.toQueryString(searchParams) > }); > > This almost works perfectly, but the range turns into "[Object > object]" and params.inspect returns this: > > {"a"=>10, "b"=>"hello", "c"=>"[object Object]", > "action"=>"get_stuff", "controller"=>"browse"} > > After reading a bit, I''ve come to understand that for nested objects > the preferred method is Object.toJSON rather than > Object.toQueryString. So I changed my code to this: > > var searchParams = { a: 10, b: "hello", c: $R(0,40) } > > var stuff = new Ajax.Request(''/browse/get_stuff'', { > method: ''get'', > parameters: Object.toJSON(searchParams) > }); > > But parameters turns into a single string, rather than individual > entries in params, and params.inspect returns this: > > {"action"=>"get_stuff", "{\"a\": 10, \"b\": \"hello\", \"c\": > {\"start\": 0, \"end\": 40}}"=>nil, "controller"=>"browse"} > > How can I get my object hash into Ruby as a hash? > > Thanks, > Mike. > > >-- ----- Richard Quadling Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731 "Standing on the shoulders of some very clever giants!" --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
All you need is turn range object into an array by calling it''s toArray method: new Ajax.Request(''/browse/get_stuff'', { parameters: { a: 10, b: ''helo'', c: $R(0,40).toArray() }, ... }) - kangax --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Hello folks, Thanks very much for the suggestions - below are some comments: On Feb 6, 4:13 am, "Richard Quadling" <rquadl...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> Unsure, but try this ... > > var searchParams = { a: 10, b: "hello", c: Object.toJSON($R(0,40)) };This makes the ruby params hash look like this: {"a"=>"10", "b"=>"hello", "c"=>"{\"start\": 0, \"end\": 40}", "action"=>"get_houses", "controller"=>"browse"} Ajax.Request can nicely send objects that do not have nested objects to a rails controller such that the action params hash will be set correctly. In this case you''ve turned the embedded object into a string which was passed back correctly, but "c => start" and friends cannot be accessed normally in the controller. On Feb 6, 8:27 am, kangax <kan...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> All you need is turn range object into an array by calling it''s > toArray method: > > new Ajax.Request(''/browse/get_stuff'', { > parameters: { a: 10, b: ''helo'', c: $R(0,40).toArray() }, > ... > })With this the query becomes: http://localhost:3000/browse/get_houses?a=10&b=helo&c=0&c=1&c=2&c=3&c=4&c=5&c=...39&c=40 which surprised me quite a bit I must admit ;o) Of course, this makes the ruby params hash become this: {"a"=>"10", "b"=>"helo", "c"=>"0", "action"=>"get_houses", "controller"=>"browse"} which is not the intent. One more note - the javascript object I wish to pass back to the server is quite a bit more complicated that the example I gave, and contains a list of sub-objects that have various parameters which store the state for the client-side javascript application. Changing the object itself would make my javascript significantly more cumbersome, and I''d rather not pull out all of the sub-objects and pass them back. I was thinking that there was a standard way to pass JSON to rails from the browser, one that could easily deal with nested objects. Since the toJSON method nicely handles nested objects, I thought my original suggestion was the obvious solution but rails will not parse the returned parameters. The person who filed this bug has a similar requirement to mine: http://dev.rubyonrails.org/ticket/7494. He wanted to use toQueryString to pass nested objects, but near the bottom of the ticket mislav suggests that one "should seek rich data functionality with JSON when we''re not serializing forms" and closes the ticket as "wontfix". I''m fine with using JSON, but I cannot get it to work. Thanks again for all the help, Mike. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Hello again, After some more investigation, I discovered that Rails does not "automatically" parse JSON as I has assumed. Instead you need to use the JSON gem described at http://json.rubyforge.org/. I guess I was so stuck on the "automatic" that I didn''t search for gems and such. The JSON gem works quite well and resolves my issue nicely. Thanks, Mike. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
On Feb 6, 2008 1:36 PM, Mike Pelley <mikepelley-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Hello folks, > > Thanks very much for the suggestions - below are some comments: > > On Feb 6, 4:13 am, "Richard Quadling" <rquadl...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> > wrote: > > Unsure, but try this ... > > > > var searchParams = { a: 10, b: "hello", c: Object.toJSON($R(0,40)) }; > > This makes the ruby params hash look like this: > > {"a"=>"10", "b"=>"hello", "c"=>"{\"start\": 0, \"end\": 40}", > "action"=>"get_houses", "controller"=>"browse"} > > Ajax.Request can nicely send objects that do not have nested objects > to a rails controller such that the action params hash will be set > correctly. In this case you''ve turned the embedded object into a > string which was passed back correctly, but "c => start" and friends > cannot be accessed normally in the controller. > > On Feb 6, 8:27 am, kangax <kan...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > All you need is turn range object into an array by calling it''s > > toArray method: > > > > new Ajax.Request(''/browse/get_stuff'', { > > parameters: { a: 10, b: ''helo'', c: $R(0,40).toArray() }, > > ... > > }) > > With this the query becomes: > > http://localhost:3000/browse/get_houses?a=10&b=helo&c=0&c=1&c=2&c=3&c=4&c=5&c=...39&c=40Then { ...., b: "helo", "c[]": $R(0,40).toArray() } will generate ...&c[]=0&c[]=1... Which should end up generating an array in the params[:c] hash ([0, 1, 2, 3, 4...]). Best, -Nicolas> which surprised me quite a bit I must admit ;o) Of course, this makes > the ruby params hash become this: > > {"a"=>"10", "b"=>"helo", "c"=>"0", "action"=>"get_houses", > "controller"=>"browse"} > > which is not the intent. > > > One more note - the javascript object I wish to pass back to the > server is quite a bit more complicated that the example I gave, and > contains a list of sub-objects that have various parameters which > store the state for the client-side javascript application. Changing > the object itself would make my javascript significantly more > cumbersome, and I''d rather not pull out all of the sub-objects and > pass them back. > > I was thinking that there was a standard way to pass JSON to rails > from the browser, one that could easily deal with nested objects. > Since the toJSON method nicely handles nested objects, I thought my > original suggestion was the obvious solution but rails will not parse > the returned parameters. > > The person who filed this bug has a similar requirement to mine: > http://dev.rubyonrails.org/ticket/7494. He wanted to use > toQueryString to pass nested objects, but near the bottom of the > ticket mislav suggests that one "should seek rich data functionality > with JSON when we''re not serializing forms" and closes the ticket as > "wontfix". I''m fine with using JSON, but I cannot get it to work. > > Thanks again for all the help, > Mike. > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Hey Nicolas,> > On Feb 6, 8:27 am, kangax <kan...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > All you need is turn range object into an array by calling it''s > > > toArray method: > > > > > > new Ajax.Request(''/browse/get_stuff'', { > > > parameters: { a: 10, b: ''helo'', c: $R(0,40).toArray() }, > > > ... > > > }) > > > > With this the query becomes: > > > > http://localhost:3000/browse/get_houses?a=10&b=helo&c=0&c=1&c=2&c=3&c... > > Then { ...., b: "helo", "c[]": $R(0,40).toArray() } will generate > > ...&c[]=0&c[]=1... > > Which should end up generating an array in the params[:c] hash ([0, 1, > 2, 3, 4...]).That is interesting - I didn''t think of that. Unfortunately, I didn''t want to expand the range (as they can become very large) but rather have the javascript range object, which looks something like "{ start: 0, end: 40, exclusive: false }", translated into a ruby object on the server side. If I pass the range object by itself, everything works great. It''s only when I embed it in another object that it fails (until I switched to parsing JSON in my controller). Thanks, Mike. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---