2
votes

say I want to work with a array where the values are maps.

IE

[ %{ "foo" => "bar"}, %{ "foo" => "baz"} ]

I can get this to work with.

 <input name = "post[list][][foo]" value = "bar">
 <input name = "post[list][][foo]" value = "baz">

Ok so that works but what if I want to add more keys to one of the maps or both?

This is where things fall apart for me.

<input name = "post[list][][email]" value = "[email protected]">
<input name = "post[list][][primary]" value = "false">

<input name = "post[list][][email]" value = "[email protected]">
<input name = "post[list][][primary]" value = "true">

I end up with an array of 4 items like so.

[%{"email" => "[email protected]"}, %{"primary" => "false"}, %{"email" => "[email protected]"}, %{"primary" => "true"}]

When what I really wanted was an array of two items like so.

[%{"email" => "[email protected]", "primary" => "false"}, %{"email" => "[email protected]", "primary" => "true"}]

Notice that each maps has two keys email, and primary as the desired map. IE %{"email" => "[email protected]", "primary" => "false"} as opose to what I ended up with which was an array of 4 maps each with one key.

Now I know this has to do with the way I named my inputs. Can anyone here help me?

FYI the context of this array and map is from Elixir.

1
I had a similar question here -> stackoverflow.com/questions/47628425/… and I guess there is no way to handle it easily, looks like adding indexes and then itirating through them is the way to go.NoDisplayName

1 Answers

2
votes

I bet you have index when iterating to render inputs.

You can render inputs like this with index.

<input name = "post[list][0][email]" value = "[email protected]">
<input name = "post[list][0][primary]" value = "false">

<input name = "post[list][1][email]" value = "[email protected]">
<input name = "post[list][1][primary]" value = "true">