bigskysoftware/hypermedia-systems-old

HTML for bulk delete functionality seems to contain an error

Closed this issue · 2 comments

First off: awesome book.

As for the issue. I'm unsure if I'm missing something since I found no issue for this in the repo, but in the book, for the bulk delete functionality, it says that adding a checkbox input to all rows in the contacts table with the name selected_contact_ids, like so:

<tr>
    <td><input type="checkbox" name="selected_contact_ids" value={"#{contact.id}"} /></td>
    ...etc.

And wrapping the table in a form tag, would give us, once the form is submitted, the following parameters (using elixir syntax here):

%{
  "selected_contact_ids" => ["1", "2", "3"]
}

In other words, a list with all the selected contacts's ids.

I, for one, never got that. It would always submit the last selected contact. In order to get the desired behavior I had to change the name attribute to be selected_contact_ids[].

I guess I'm wondering if this is a typo or if there is something I'm not seeing.

1cg commented

HTML will submit all the parameters with the same name. Some server side frameworks will present this as an array, some, apparently, only present the last one. (I bet there is a way to get all values for the parameter name.)

So the code is correct, but you may need to research your backend to see how to access the values correctly.

Yeah, I had to write the name as I said (selected_contact_ids[]).

Indeed, I haven't tested this with any other backend, but for anyone coming across this in the future, this is how you get the array of ids using Phoenix