Forum: Ruby on Rails Create ActiveRecord from Javascript object via JSON

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
E82dbf794da5692e0f3ed1d338f82cfb?d=identicon&s=25 Adam Chester (chester122)
on 2009-01-05 13:37
HI All,
I am trying to create an ActiveRecord object from a javascript object
via JSON. However the controller fails to set the variables passed
within the ActiveRecord object.

As an example, a person object has two fields: firstname and lastname.

The JSON generated by the JSON.stringify function of the JSON.org
library produces:
{"firstname" : "Joe" , "lastname" : "Bloggs"}

However the controller requires:

{ "Person" : {"firstname" : "Joe" , "lastname" : "Bloggs"} }

I am aware that in the normal course of events (for HTTP requests) the
parameters for the request are nested under the class name of the model
being created.

What would be the neatest way for the controller to process the JSON
requests as generated? Or alternatively how do you generate "correct"
JSON from Javascript objects for passing to your controllers?

TIA,
Adam
This topic is locked and can not be replied to.