Column restoration models?

Let's say I have the following form, including a model and a nested model:

<label>Company Name</label> <input type="text" ng-model="company.name" /> <label>Owner Name</label> <input type="text" ng-model="company.owner.name" /> 

Who I write as follows:

 Restangular.all('companies').post($scope.company); 

What I expect at the end of the server (in this case Rails) is a nested hash, something like this:

 company: name: Test Company owner: name: Test Owner 

But I get the following:

 name: Test Company company: name: Test Company owner: name: Test Owner 

It seems that the models are flattened, and also the fields from the first model are repeated outside the field of view.

How can I publish a model while preserving its nesting and, preferably, not repeating the fields of models outside its area in the hash?

+6
source share
2 answers

I am the creator of Restangular.

Could you console.log print $ scope.company?

Restangular doesn't flatter anything. It just sends the exact JSon that you provided as the parameter, so you should check what the result of $ scope.company is.

After that we can check it further.

Also, have you checked the network tab for the request payload? Everything is good?

+1
source

I feel the need to clarify this if someone else finds this question.

Passing $scope.company passes the JS object, which is the company , which does not include the name of the scope variable itself:

 { name: 'Test Company', owner: { name: 'Test Owner' } } 

The server will see this as a POST variable named name , which is a string with the value "Test Company" and another variable named owner , which is an array with an index named name with the value 'Test Owner `

In PHP, it will be as follows:

 $_POST['name']; // would = 'Test Company' $_POST['owner']; // would = array('name'=>'Test Owner') 

If you want this to be a server-side property array called company , you need to encapsulate $scope.company into a JS object using a property called company:

 $scope.company = { company: { name : 'Test Company', owner : { name : 'Test Owner' } } }; 

Now on the server side you will find the following:

 $_POST['company']; // would = array('name'=>'Test Company','owner'=>array('name'=>'Test Owner')) 
0
source

Source: https://habr.com/ru/post/949224/


All Articles