Rails “array” in xml to params

蹲街弑〆低调 提交于 2019-12-08 06:46:30

问题


I am designing a rails rest API that I would like to expose either as xml or json. In a specific action, I am submitting a collection of objects like this:

curl -d "<posts><post><title>my title</title></post><post><title>second post</title></post></posts>" localhost:3000/posts -X POST

I thought that this would be translated into

> params[:posts]
[{:title => "my title"}, {:title => "second post"}]

Instead of that I get

 {:post => [{:title => "my title"}, {:title => "second post"}]}

The question is then how should I format my xml such that I get this in params[:posts] ?

[{:title => "my title"}, {:title => "second post"}]

I know how to do it in functional tests and json but not xml :(


回答1:


Looks like the XML in your curl starts with:

<posts></post>...

rather than:

<posts><post>...

Edit:

Posting XML as an array in Rails requires type="array" to be added to the array node.

<posts type="array"><post>...


来源:https://stackoverflow.com/questions/5424063/rails-array-in-xml-to-params

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!