On Feb 6, 2008 1:36 PM, Mike Pelley <[EMAIL PROTECTED]> wrote:
>
> Hello folks,
>
> Thanks very much for the suggestions - below are some comments:
>
> On Feb 6, 4:13 am, "Richard Quadling" <[EMAIL PROTECTED]>
> 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 <[EMAIL PROTECTED]> 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
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...]).
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 [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---