English 中文(简体)
铁路3.2 -- -- 主动资源 -- -- 与JSON公司使用POST/PUT
原标题:Rails 3.2 - ActiveResource - Using POST/PUT with JSON

我观察了一段时间,但目前却很失落。 我将问题缩小到与JSON的关系上,而JSON似乎并没有以清晰的方式发送。

我们以前曾使用过铁路3.0.7(我将要描述的问题并不存在 ) 。 我们需要升级铁路来利用一些新的功能,但现在有这个问题。 我几乎可以肯定,现在它是一个简单的配置或什么的,但我需要更多关注它。

我们正在使用主动资源对象, 当设定对象值并保存它( 从而生成 PUT) 时, 我们得到 ab 403: 禁止从远程网络服务中返回。 这在网络服务层中不是一个问题 。

在创建新对象和制作 POST 时, 我们正在获得一个 409 个: 当它试图以默认值更新数据库时, 冲突返回了 - 这意味着 Json 数据没有到达服务范围 - 或者无法读取 。

我的问题是,这在铁路3.0.7中有效,在3.2.3(或3.1.3(我也曾尝试过))中不再有效。 我愿意接受JSON数据以外的建议,但是我非常肯定,这正是问题所在。

我的主动资源模式

self.format = :json

我的初始化文件之一有以下内容:

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

谢谢你的建议!

<强> UPDATE

我用卷发做测试 让它工作的方式是:

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

我得到了这个工作,但

  1. 似乎必须明确设置内容- 类型标题, 否则它会导致 415: Media 未知错误 。

  2. 如果我在活动资源对象中设置内容-类型标题, 我仍然会得到409:冲突错误—— 这让我相信JSON还没有正式组成。

<强 > EDIT(再次)

I have figured out where the issue with the JSON is - Rails is keeping the root in the JSON. Thus instead of sending the resource back as

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

它正在发送

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

然而,我设置了 包括_root_in_json 虚假 - 我在初始化文档中尝试过, 直接在模型本身中尝试过, 没有变化 。

最佳回答

所以我“固定”了这个(围绕它工作), 超越了我模型中的 to_json 方法, 简单地返回 self.attriptes.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
问题回答

似乎主动资源: Base. 包括_ root_in_json 在3-2号铁路(包括3.2.13)中被打破。 这个问题已经在独立的主动资源回收站中固定了 :

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

你可以做同样的事情 在你的初始化器中修补猴子。

<强度>更新 :这个问题已经固定在3-2稳定的铁路上





相关问题
JQuery/MVC Search Issue

I have inherited a piece of work where the entry screen shows a summary of 20 calculated variables. E.g. Var A (250), Var B (79). Clicking on any of these links takes the user to a view with a ...

jQuery quicksearch plug-in tinkering with JSON

I ve implemented the quicksearch plugin by Rik Lomas and I love it for an application in a custom CMS I m building. I was wondering though, since I m going to have a bizillion items in the table if ...

JSON with classes?

Is there a standardized way to store classes in JSON, and then converting them back into classes again from a string? For example, I might have an array of objects of type Questions. I d like to ...

PHP json_decode question

i m trying to use json_decode to combine a few json objects and then re-encode it. my json looks like: { "core": { "segment": [ { "id": 7, "...

Converting JSON data to Java object

I want to be able to access properties from a JSON string within my Java action method. The string is available by simply saying myJsonString = object.getJson(). Below is an example of what the string ...

热门标签