Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

116
Visualizações
Check Box adding 2 entries to Payload

I've the following in ruby file html.erb

<div class="hidden" id="contractor-group"> 
   <%= f.check_box :contractor, id: 'int_contractor_check'%>
   <label for="contractor">Internal Contractor</label>
</div>

function enableContractor() {
  var type = $('#company-type').val();
  if (type == 'INTERNAL') {
     $('#contractor-group').removeClass('hidden')
     $('#int_contractor_check').prop('checked', false);
  }
  else {
     $('#int_contractor_check').prop('checked', false);
     $('#contractor-group').addClass('hidden')
  }
}

A dropdown change to INTERNAL toggles the checkbox hidden/shown

id: "company-type",
onchange: "enableContractor()",

In the payload I get double contractor entries. Any ideas what I am doing wrong? enter image description here

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Nothing wrong here. For checkboxes Rails creates a hidden field with 0 value (you can check it in generated html). It's needed b/c checkbox value is send only when checkbox is checked. So, to avoid having nothing send, when it's not checked, we need this hidden input. When parsing it on the server, Rails will overwrite 0 with the latest value (1) See docs

about 4 years ago · Juan Pablo Isaza Relatório

0

I stripped down the code to the following and it's working fine now. Using true and false was better to use on server side anyway. Not sure why the server wasn't using the value 1 ever,

Thanks @faron for the link, something I didn't know.

<div class="hidden" id="contractor-group">
   <%= f.check_box :contractor, { checked: false }, true, false %>
    <b> Internal Contractor </b>
</div>
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda