Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

125
Views
Hide remove link for single input field

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.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!