Formato relacionamento NxN Rails em Formulários
MODEL:
CONTROLLER:
HTML:
PARAMS:
accepts_nested_attributes_for :plantao_lotacoes
CONTROLLER:
params.require(:plantao).permit(:data_referencia,:plantao_lotacoes_attributes => [:id, :periodo_inicio, :periodo_fim, :servidor_id, :contato_id, :lotacao_id])
HTML:
<form method="post" id="new_plantao" class="new_plantao" action="/plantoes" accept-charset="UTF-8"><div style="margin:0;padding:0;display:inline"><input type="hidden" value="✓" name="utf8"><input type="hidden" value="nDNRre5UBxWZZEKLVm6c0XpeVzQBJL9+jDXecrYCA4g=" name="authenticity_token"></div>
<div class="field">
<label for="plantao_data_referencia">Data referencia</label><br>
<select name="plantao[data_referencia(3i)]" id="plantao_data_referencia_3i">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14" selected="selected">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
<option value="31">31</option>
</select>
<select name="plantao[data_referencia(2i)]" id="plantao_data_referencia_2i">
<option value="1">Janeiro</option>
<option value="2">Fevereiro</option>
<option value="3" selected="selected">Março</option>
<option value="4">Abril</option>
<option value="5">Maio</option>
<option value="6">Junho</option>
<option value="7">Julho</option>
<option value="8">Agosto</option>
<option value="9">Setembro</option>
<option value="10">Outubro</option>
<option value="11">Novembro</option>
<option value="12">Dezembro</option>
</select>
<select name="plantao[data_referencia(1i)]" id="plantao_data_referencia_1i">
<option value="2009">2009</option>
<option value="2010">2010</option>
<option value="2011">2011</option>
<option value="2012">2012</option>
<option value="2013">2013</option>
<option value="2014" selected="selected">2014</option>
<option value="2015">2015</option>
<option value="2016">2016</option>
<option value="2017">2017</option>
<option value="2018">2018</option>
<option value="2019">2019</option>
</select>
</div>
<div>Diretoria de São Paulo</div>
<input type="text" name="plantao[plantao_lotacoes_attributes][0][servidor_id]" id="plantao_plantao_lotacoes_attributes_0_servidor_id">
<div>Diretoria de Piauí</div>
<input type="text" name="plantao[plantao_lotacoes_attributes][1][servidor_id]" id="plantao_plantao_lotacoes_attributes_1_servidor_id">
<div>Diretoria de Ceará</div>
<input type="text" name="plantao[plantao_lotacoes_attributes][2][servidor_id]" id="plantao_plantao_lotacoes_attributes_2_servidor_id">
(...)
<div class="separator"></div>
<div class="form-actions">
<button type="submit" class="btn btn-primary">
<i class="fa fa-check-circle"></i>
Salvar
</button>
<a href="/plantoes" class="btn btn-default">
<i class="fa fa-times"></i>
Cancelar
</a> </div>
</form>
PARAMS:
{"data_referencia(3i)"=>"1", "plantao_lotacoes_attributes"=>{"0"=>{"servidor_id"=>"1"}, "1"=>{"servidor_id"=>""}, "2"=>{"servidor_id"=>""}, "3"=>{"servidor_id"=>""}, "4"=>{"servidor_id"=>""}, "5"=>{"servidor_id"=>""}, "6"=>{"servidor_id"=>""}, "7"=>{"servidor_id"=>""}, "8"=>{"servidor_id"=>""}, "9"=>{"servidor_id"=>""}, "10"=>{"servidor_id"=>""}, "11"=>{"servidor_id"=>""}, "12"=>{"servidor_id"=>""}, "13"=>{"servidor_id"=>""}, "14"=>{"servidor_id"=>""}, "15"=>{"servidor_id"=>""}, "16"=>{"servidor_id"=>""}, "17"=>{"servidor_id"=>""}, "18"=>{"servidor_id"=>""}, "19"=>{"servidor_id"=>""}}}
Comentários
Postar um comentário