Reputation: 1244
I'm witnessing an odd behavior in Rails, when sending a post request, with the following body:
If you check is a Hash (converted to JSON when sending). But ODDLY when being read by params in controller, is recognized like this:
If you check carefully, the attributes of :first_name
and :email
are moved to the previous item in the array.
I'd thought if you have an array with certain attributes on the first item, but some different attributes on the following items, the array would be respecting the positions of which the attributes are set for.
I know most likely the answer would be "just put a nil
or empty value of those attributes on the first item of the array", but I'm more interested in the reason of why this is happening.
Thank you.
Thanks to a question, I replicated the scenario form a browser (I was originally doing the request from the test
feature of rails), and checking the Network from the browser, this is what is being sent:
{
"name": "un nombre",
"team_members": [
{
"user_id": 1,
"team_member_role_id": 4
},
{
"email": "[email protected]",
"first_name": "Cap America",
"team_member_role_id": 4,
"admin": true
},
{
"email": "[email protected]",
"first_name": "Bruce Banner",
"team_member_role_id": 1,
"admin": false
},
{
"email": "[email protected]",
"first_name": "Tony Stark",
"team_member_role_id": 1,
"admin": false
},
{
"email": "[email protected]",
"first_name": "Thor Hijo de Odín",
"team_member_role_id": 2,
"admin": false
}
]
}
And it works. So I focused on how I was sending the info from the test environment, this is the actual code:
team = {
:name => 'un nombre',
:team_members => [
{
:user_id => 1,
:team_member_role_id => TeamMemberRole.role_communicator_id
},
{
:email => '[email protected]',
:first_name => 'Cap America',
:team_member_role_id => TeamMemberRole.role_communicator_id,
:admin => true
},
{
:email => '[email protected]',
:first_name => 'Bruce Banner',
:team_member_role_id => TeamMemberRole.role_visionary_id,
:admin => false
},
{
:email => '[email protected]',
:first_name => 'Tony Stark',
:team_member_role_id => TeamMemberRole.role_visionary_id,
:admin => false
},
{
:email => '[email protected]',
:first_name => 'Thor Hijo de Odín',
:team_member_role_id => TeamMemberRole.role_developer_id,
:admin => false
}
]
}
post create_team_path, :team => team, :format => :json
And what is being read in the controller by request.raw
, this is what is getting (using the debugger):
team[name]=un+nombre&
team[team_members][][user_id]=1&
team[team_members][][team_member_role_id]=4&
team[team_members][][email]=cpamerica%40avengers.com&
team[team_members][][first_name]=Cap+America&
team[team_members][][team_member_role_id]=4&
team[team_members][][admin]=true&
team[team_members][][email]=hulk%40avenrgers.com&
team[team_members][][first_name]=Bruce+Banner&
team[team_members][][team_member_role_id]=1&
team[team_members][][admin]=false&
team[team_members][][email]=ironman%40avengers.com&
team[team_members][][first_name]=Tony+Stark&
team[team_members][][team_member_role_id]=1&
team[team_members][][admin]=false&
team[team_members][][email]=thor%40avengers.com&
team[team_members][][first_name]=Thor+Hijo+de+Od%C3%ADn&
team[team_members][][team_member_role_id]=2&
team[team_members][][admin]=false&
format=json
Any idea on why the index of each team_member
is missing? Am I sending the array wrong?
Upvotes: 0
Views: 130
Reputation: 353
To send an acceptable json request you need to send as a formed String instead the hash, also add the headers your code must look something like this:
post create_team_path, {:team => team}.to_json, 'CONTENT_TYPE' => 'application/json;charset=utf-8'
Note the problem is within the parser hash to form-request not adding the index of each object
Source: How to post JSON data in rails 3 functional test
Upvotes: 1