0

I am sending a POST request to my Rails API with the following params:

  "user"=>{"first_name"=>"Gerry", "last_name"=>"Moen", "email"=>"gerrymoen@test.com", "addresses"=>[{"address_type"=>"home"}, {"address_line_1"=>"25609 Littel Island", "address_line_2"=>nil, "city"=>"Chill", "state"=>"CA", "zip"=>"12345", "address_type"=>"mailing"}]}

However when I check the params in my API endpoint they appear like so:

  "user"=>{"first_name"=>"Gerry", "last_name"=>"Moen", "email"=>"gerrymoen@test.com", "addresses"=>[{"address_type"=>"home", "address_line_1"=>"25609 Littel Island", "address_line_2"=>nil, "city"=>"Chill", "state"=>"CA", "zip"=>"12345"}, {"address_type"=>"mailing"}]}

As you can see the mailing address fields are sent as part of the home address. Now I know nested attributes are supposed to be assigned as addresses_attributes. I have this in my user_params method to handle that.

        params[:user][:addresses_attributes] = params.dig(:user).delete(:addresses)
        params.require(:user).permit(....)

But my question is what is Rails doing behind the scenes to switch around my address attributes like that. I don't see anything apparent in my code that is doing that.

EDIT: Ok found some things. I printed out the unaltered POST data. It looked like this

"user[first_name]=Gregorio&user[last_name]=Douglas&user[email]=gregoriodouglas%40test.com
&user[addresses][][address_type]=home&user[addresses][][address_line_1]=458%20Karlene%20Greens&user[addresses][][address_line_2]=&user[addresses][][city]=Superior&user[addresses][][state]=MT&user[addresses][][zip]=59872&user[addresses][][address_type]=mailing"

I believe the reason why it was conflating the addresses was because they were sent as an array of hash attributes. I believe in order to get them correctly assigned I'd need to set an index in the array. Or something.

Gabriel
  • 621
  • 7
  • 19
  • can you share the relevant form part? – Joel Blum May 26 '20 at 19:11
  • It isn't a form. I'm sending params to an API endpoint. I'm formatting the data in a JSON request to create a user. I shared the relevant JSON data getting sent through. – Gabriel May 26 '20 at 19:14
  • did u check the network to see what was being sent? – Joel Blum May 26 '20 at 19:21
  • I edited my question. I discovered something that I think explains what is happening somewhat. – Gabriel May 26 '20 at 19:39
  • Can you please explain how you are sending this POST request because that is a Hash so something must be converting this to JSON and the second part looks like a GET request although to be fair I have never used the `raw_post` method – engineersmnky May 26 '20 at 20:34
  • Not doing anything strange with my POST request. Putting the params I have at the first part of my question in a post request body using HTTParty. There is literally nothing extraordinary I'm doing there. I think the confusion with the "EDIT" part of my question is based on how `raw_post` displays the POST data. – Gabriel May 26 '20 at 21:40

0 Answers0