I'm trying to POST some data to my Rails 4 API.
The resource:
App.factory 'House', ['$resource', ($resource) ->
$resource '/api/v1/houses/:id', { id: '@id' }
]
The JSON representation of the resource:
newHouse = {
"owner_id": "30",
"name": "test",
"address_attributes": {
"street": "somewhere",
"city": "on",
"region": "earth"
}
}
On House.save(null, $scope.newHouse)
, the server logs give me this:
Processing by Api::V1::HouseController#create as JSON
Parameters: {"owner_id"=>"34", "name"=>"test", "address_attributes"=>{"street"=>"somewhere", "city"=>"on", "region"=>"earth"}, "house"=>{"name"=>"test", "owner_id"=>"34"}}
Which is underisable, to say the least.
owner_id
andname
appear directly bellow root, and below "house" - I would expect only below "house"address_attributes
appears only directly bellow root - I would expect it to be belowhouse
Basically I want this:
Processing by Api::V1::HouseController#create as JSON
Parameters: {"house"=>{"name"=>"test", "owner_id"=>"34", "address_attributes"=>{"street"=>"somewhere", "city"=>"on", "region"=>"earth"}}}
Any help?
EDIT
Rails controller action:
def create
house = House.new house_params
if house.save
head 200
else
render json: {
"error" => "validation errors",
"detail" => house.errors.messages
}, status: 422
end
end
def house_params
fields = [:name, :owner_id, address_attributes: [:street, :city, :region, :country, :postal_code ]]
params.require(:house).permit(fields)
end
The model House
has:
has_one :address
accepts_nested_attributes_for :address
I don't want to change the way the server handles the data. Many backends expect parameters to be held in the format I want, and even jQuery does it in its AJAX calls. AngularJS should be the one to change, or at least allow ways to configure.