Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8230491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T17:09:18+00:00 2026-06-07T17:09:18+00:00

Is there a way to count the number of nested fields that have been

  • 0

Is there a way to count the number of nested fields that have been added to a form, in Rails?

I currently have a form that allows the user to add or remove nested attributes.

The code is basically the same from the Railscast http://railscasts.com/episodes/196-nested-model-form-revised just changed slightly for my requirements (different controller and model names).

What I want to be able to do is count the number of sets of nested attributes that are currently in the form. Is there a way to do this in Rails or would I be better using jQuery?

I need to be able to access the value in the view.

Any help much appreciated. If you need to see the code, let me know.

jQuery ->
  $('form').on 'click', '.remove_fields', (event) ->
    count--
    $(this).prev('input[type=hidden]').val('1')
    $(this).closest('.panel').hide()
    $('#price').text(count)
    event.preventDefault()

  $('form').on 'click', '.add_fields', (event) ->
    count++
    $('#price').text(count)
    time = new Date().getTime()
    regexp = new RegExp($(this).data('id'), 'g')
    $(this).before($(this).data('fields').replace(regexp, time))
    event.preventDefault()

In the view I have set the count variable to 1, for testing purposes. I’m thinking that this needs to be set before the page loads as it is probably loading the javascript last, therefore not showing the initial count?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T17:09:22+00:00Added an answer on June 7, 2026 at 5:09 pm

    If you have an element in which you want to display the count variable, say <div id="count"></div>, you can write $('#count').html(count) in the click function right after count gets updated.

    UPDATE:
    for the initial count (before the user clicks the add/remove button), include the count in erb page template

    <div id="count"><%= @initial_count %></div>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way that I can get the count number for an iterator?
Is there any way to count the number of currently connected clients in a
Is there a way I can programmatically count the number of links for a
I was wondering if there was any possible way to get the number (count)
Is there an easy way to count the number of pages is a Word
Is there an easy way to count the number of active users or guests
Is there a simple way to count the number of occurrences of all elements
Is there a way to count the number of <li> tags in a text
Is there a way to count the number of pages returned by the twitter
Is there a way to count how many times an image how been clicked.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.