Rails “array” in xml to params

2019-06-05 08:19发布

问题:

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>...