I am doing some tests with a simple rails app. Next to that I’ve created an ActiveResource object that connects to this application to use it’s api.
But I want/need to use json format instead of the default (=xml).
I’ve come to the conclusion that out-of-the-box that ActiveResource in json is broken when doing updates (put requests) or I’m doing something wrong
A sample application can be found here: https://github.com/khelben/activeresource_test
More in detail, the ActiveResource tests can be found in lib/employee.rb: https://github.com/khelben/activeresource_test/blob/master/lib/employee.rb
All tests consists of the same steps:
-
creating an employee
-
updating its name
I try these steps in both xml and json format, here are my conclusions:
-
XML create and update work fine!
-
JSON create works fine!
-
JSON update fails!
From what I can see from the rails server output is that the params has some unusual attributes:
This is the complete data provided with the put request:
Parameters: {“employee”=>{“name”=>“Randy”, “id”=>“44”, “employee”=>{“employee”=>{“name”=>“Randy”, “created_at”=>“2011-02-24T11:20:22Z”, “updated_at”=>“2011-02-24T11:20:22Z”, “id”=>44, “full_name”=>nil}}, “full_name”=>“Something”}, “id”=>“44”}
It is strange that the params include some unnecessary attributes (indicated in bold).
So I think it’s related to have the serialization is handled, depending on the format, the next tests in the same file prove me right:
When I just change the format from XML (https://github.com/khelben/activeresource_test/blob/master/lib/employee.rb#L41) to JSON (https://github.com/khelben/activeresource_test/blob/master/lib/employee.rb#L77) the same tests produces other output?
once again, I see that when using format json, the output of both ‘#encode’ and ‘#to_json’ contains unnecessary data, which causes the server to fail to update the resource.
If someone could confirm these findings and eventually to provide some pointers onto where to solve this, I would be happy to fix this.
TIA
Christiaan