Rails 3.2 - ActiveResource - using POST / PUT with JSON

I looked at this for a while, and now I'm at a loss. I narrowed the problem down to communication with JSON and that JSON does not seem to be sent in a clear format.

Earlier we used Rails 3.0.7 (where the problem that I am going to describe did not exist). We need to upgrade Rails to take advantage of some new features, but now there is this problem. I'm pretty sure that at the moment this is a simple configuration or something like that, but I need to take a look at it even more.

We use ActiveResource objects, and after setting the values ​​of the object and saving it (thus creating the PUT) we get ab 403: Forbidden is returned from the remote web service. This is not a web service level issue.

When creating a new object and creating a POST, we get a returned 409: Conflict when it tries to update the database with default values, which means that json data does not reach the service or is not read.

My problem is that this worked in Rails 3.0.7 and no longer works in 3.2.3 (or 3.1.3, which I also tried). I am open to suggestions other than JSON data, however I am sure that this is exactly the problem.

My ActiveResource model has

self.format = :json

One of the initializer files has the following:

ActiveResource::Base.include_root_in_json = false
ActiveSupport::JSON.backend = "json_gem" 

Thanks for any suggestions!

UPDATE

I tested with curl and made it work:

curl -H "Content-Type: application/json" -d "{\"userID\":\"xxx\", \"label\":\"doc.test.test\", \"value\":\"1,19\", \"id\":\"-1\"}" -v -X POST http://host:port/usermanagement/users/xxx/prefs

I did it, but

  • It seems that the Content-Type header should be explicitly set, otherwise it will result in error 415: Media Unrecognized

  • Content-Type ActiveResource, 409: Conflict, , JSON .

EDIT ()

, JSON - Rails JSON. ,

{"name":"value", "name":"value"}

{"ResourceName": {"name":"value", "name":"value"}}

include_root_in_json false - , , .

+5
2

, "" ( ), to_json , self.attributes.to_json

class Document < ActiveResource::Base
  self.site += 'user/:user_id'
  self.element_name = "docs"

  def to_json(options={})
    self.attributes.to_json(options)
  end
end
+1

, ActiveResource:: Base.include_root_in_json Rails 3-2-stable ( 3.2.13). ​​ activeresource:

def to_json(options={})
  super(include_root_in_json ? { :root => self.class.element_name }.merge(options) : options)
end

, .

: ​​ rails 3-2-stable backport commit

+1

All Articles