How to serialize nested form data and submit to Rails app

184 views
Skip to first unread message

Jianbo Cui

unread,
Jun 13, 2013, 10:45:09 PM6/13/13
to ang...@googlegroups.com
Hi,

   I know how to submit unnested form data like following
Copied from:http://stackoverflow.com/questions/15877212/is-there-a-more-effective-way-to-serialize-a-form-with-angularjs

<form ng-controller="MyCtrl" ng-submit="submit()">
    <input type="text" name="name" ng-model="formData.name">
    <input type="text" name="address" ng-model="formData.address">
    <input type="submit" value="Submit Form">
</form>

function MyCtrl($scope) {
    $scope.formData = {};

    $scope.submit = function() {  
        console.log(this.formData);
    };
}

But this doesn't work when I have a nested form. In my Rails Model I am using accepts_nested_attribute_for, but how can I make my form so it can submit the correct paramters to my Rails controller like what we normally do with fields_for helper? My nested form data is store inside Angular Controller variable $scope.warehouse, but this doesn't gets serialized correctly when form submission.

This is want I want:
{"id"="1", "warehouse"={"duplicated warehouse name", "location_attributes"=["0"={"name"="location name", "code"="location code"}, "1"={"name"="location name2", "code"="location code2"}]}}
But I got this:
{"id"="1", "name"="duplicated warehouse name", "warehouse"={"duplicated warehouse name"}, "locations"=[{"name"="location name", "code"="location code"}]}

Sample code: http://stackoverflow.com/questions/17098269/angular-how-to-submit-a-nested-form-to-rails-app


Thanks!!
Reply all
Reply to author
Forward
0 new messages