Reputation: 3598
I have written up a Rest api for my website. Currently I have it so that a user has many computers that they can own. I am trying to send a json payload that updates the user account with a computer. For testing this is what I send:
{"auth_token"=>"AiprpscAqN6hnvNDHSwh",
"user"=>"{"id":1,
"username":"Rizowski",
"email":"[email protected]",
"computer":[{"id":0,
"user_id":1,
"name":"Desktop",
"enviroment":"Windows 8",
"ip_address":"192.168.1.10",
"account":[]}]}",
"format"=>"json",
"id"=>"1"}
Once I send it, the Rails controller receives it and parses the request into a hash using the JSON.parse method. Once I try to save the user object it says this:
ActiveRecord::AssociationTypeMismatch in Api::V1::UsersController#update
Computer(#51701664) expected, got Hash(#17322756)
Side question: I am still trying to completely understand rails and rest, but is it a good practice to send a computer object as I am? OR should I be sending the computer data through my computer api controller?
Upvotes: 0
Views: 415
Reputation: 6808
The solution to your error is to use the key computer_attributes
instead of computer
.
This is a normal practice for strongly associated and hierarchical data. If computers
are going to be updated independently of user, you should certainly use its own controller.
Upvotes: 1