Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What rules does HTML follow when constructing parameters?

I'm using Rails to auto-magically create child objects based on a complex set of nested attributes. Therefore, I need the parameters to be nested in a very particular way. Obviously I realize I can construct them however I want with JS, but I'd like the order of the form to automatically help with the construction. For context, I have 2 columns, represented by 2 <td>s. Each column can either create a new record or edit an existing record. Of course, when an existing record is to be modified, the id of the record must be passed.

The rendered HTML is as follows:

<td width="50%" style="padding-right:3%" class="logistic-details" data-type="logistics" data-typelogistics="delivery" data-instructions="test instructions" data-id="1" data-amount="20">
  <span class="area-to-inject-amount-inputs" data-object="type_logistics" data-type="logistics" data-typelogistics="delivery">
    <input class="labeler-response" name="type_logistics_attributes[][id]" type="hidden" value="1">
    <input class="labeler-response" name="type_logistics_attributes[][instructions]" type="text" value="test instructions">
  </span>
</td>

<td width="50%" style="padding-right:3%" class="logistic-details" data-type="logistics" data-typelogistics="pickup" data-instructions="" data-id="" data-amount="0">
  <span class="area-to-inject-amount-inputs" data-object="type_logistics" data-type="logistics" data-typelogistics="pickup" data-actioned="charged">
    <input type="hidden" name="type_logistics_attributes[][type_of_logistics]" value="pickup">
    <input class="injected-amount-input" type="number" min="0" max="" placeholder="Amount" name="type_logistics_attributes[][charged_amounts_attributes][][amount]" value="20">
    <span class="area-to-inject-type-of-amount">
      <input type="hidden" name="type_logistics_attributes[][charged_amounts_attributes][][type_of_amount]" value="logistics">
    </span>
    <input class="labeler-response" name="type_logistics_attributes[][instructions]" type="text" placeholder="Enter address and instructions">
  </span>                      
</td>

In this case, the first <td> is modifying an existing record with id of 1, while the second <td> is providing the parameters to create a new record. When a new record is created, child charged_amounts are also created. Thus, these are the parameters I would expect:

"type_logistics_attributes"=>[
  {"id"=>"1", "instructions"=>"test instructions"},
  {"type_of_logistics"=>"pickup", "charged_amounts_attributes"=>[{"amount"=>"40", "type_of_amount"=>"logistics"}], "instructions" => "123 Fake street"}
]

Instead, I am getting the below:

"type_logistics_attributes"=>[
  {"id"=>"1", "type_of_logistics"=>"pickup", "instructions"=>"test instructions", "charged_amounts_attributes"=>[{"amount"=>"40", "type_of_amount"=>"logistics"}]}, 
  {"instructions"=>"123 Fake street"}
]

Somehow, the <td> boundary isn't working and the child charged_amount attributes have somehow been lumped into the first <td> existing record modification.

Thanks!

like image 760
james Avatar asked Jun 03 '16 23:06

james


People also ask

What are the 3 requirements in creating a HTML page?

To create a true HTML document, you start with three container elements: <html> , <head> , and <body> . These three elements work together to describe the basic structure of your page: <html> This element wraps everything (other than the doctype) in your web page.

Which HTML tag is used to define parameters?

The <param> HTML element defines parameters for an <object> element.


2 Answers

It is not <td> or any similar HTML element that makes a "boundary" for inputs, it is the <form> element only. All inputs that are within a <form> tag are sent by the browser as parameters when submitting the form. You probably have both columns in one <form> and that is why parameters from both columns are intermixed in the params.

like image 133
Matouš Borák Avatar answered Sep 17 '22 19:09

Matouš Borák


The only way I know of by which you can get both parameters in one form but nested differently is that if you provide some id to the new input field. That is what distinguishes the elements of the type_logistics_attributes array.

I had a similar need and I used ryanb's nested form. When I use it for the same purpose as yours, what it does is, it provides a random id to the new input field. That is how it is differentiated.

Hope this guides you in the right direction.

like image 29
mansoor.khan Avatar answered Sep 19 '22 19:09

mansoor.khan