En mi aplicación Rails 5, tengo un formulario de registro donde el usuario puede elegir el tipo de registration_type de la lista desplegable. Cuando selecciona la opción Caregiver , se muestra un campo adicional en el formulario; lo maneja el archivo registrant.js a continuación:
$(document).on('focusout change', "#registrant_registration_attributes_registered_as", toggle_caregiver_fields_on_ready); function toggle_caregiver_fields_on_ready(){ var caregiverSectionElement = $("#registrant_registration_attributes_registrant_id"), register_as = $("#registrant_registration_attributes_registered_as"), caregiver = (register_as.val() == 'caregiver'); if (caregiver) { $('.patient-caregiver-section').removeClass('hidden'); $('#patient-search-results').removeClass('hidden'); } else { $('.patient-caregiver-section').addClass('hidden'); $('#patient-search-results').addClass('hidden'); } } Funciona bastante bien hasta que el formulario arroja un error que provoca la acción render :new del controlador create, como se muestra a continuación:
registrants_controller.rb
def create @registrant = Registrant.new(parse_post_params) if @registrant.save! #some logic (...) @registrant.add_special_status_to_history(current_login.user) @caregiver_patient.save! if @patient.present? redirect_to registrant_path(@registrant), notice: 'Registrant Added' else render :new end end Después de que se procesó la :new página, el archivo JS no parecía funcionar; en la consola de Chrome, se seleccionó el menú desplegable I:
<select class="form-control required" aria-required="true" name="registrant[registration_attributes][registered_as]" id="registrant_registration_attributes_registered_as"><option value=""> Please select </option> <option selected="selected" value="caregiver">Caregiver</option> <option value="patient">Patient</option></select> Pero la clase patient-caregiver-section todavía tiene un valor hidden :
<div class="patient-caregiver-section hidden"> <div id="add-patient"> <div class="form-group"><label class="non-bold-label pull-left" for="registrant_registration_attributes_patient_to_caregiver"> Patient Added to Caregiver </label> </div> </div> </div>¿Cómo forzar el archivo JS para eliminar esta clase también cuando se procesó una nueva acción?