In my rails app, I have used nested form fields gem for adding and removing contacts fields in my Employee creation form. HTML for contact fields looks like this:
<h3 class= "fw-bold mt-2">Contacts:</h3>
<div class="nested_form_field">
<%= f.nested_fields_for :contacts do |contact| %>
<div class="fields-row">
<%= contact.label :number, class: "m-1"%>
<%= contact.text_field :contact_no, placeholder: "Enter Contact", class: "m-1" %>
<%= contact.remove_nested_fields_link 'Remove', data: { confirm: 'Are you sure?' }, class: "link-danger remove_fields" %>
</div>
<% end %>
</div>
<%= f.add_nested_fields_link :contacts, 'Add Contact', class: 'btn btn-secondary btn-sm mt-1' %>
Here, I want to make changes to the view such that when the input field for contact number is only one, the link to remove contact should hide. For that, I have added this code to application.js:
$(document).ready(function (){
if ($(".fields-row").length == 1) {
$(".remove_fields").hide();
}
})
So when the page loads, there is no link to remove, but after adding multiple fields, each field gets a link to remove except first one. I want to change the js such that even first contact can be removed and when there is only one contact available, the link should hide. Please help.