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 6207397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:37:21+00:00 2026-05-24T05:37:21+00:00

i am following Ryan Bates episode on nested form fields and have added the

  • 0

i am following Ryan Bates episode on nested form fields and have added the bit of jquery suggested at the end of part 2. Everything works well(i am able to add fields and remove fields). i now want to limit the number of fields you can add in the form. in my application.js i have

function add_fields(link, association, content) {
        var new_id = new Date().getTime();
        var regexp = new RegExp("new_" + association, "g")
        $(link).parent().before(content.replace(regexp, new_id));
}

as Ryan Bates has written. reading another post i changed the lines to now read:

function add_fields(link, association, content) {
    if($(".fields input").length < 5) {
        var new_id = new Date().getTime();
        var regexp = new RegExp("new_" + association, "g")
        $(link).parent().before(content.replace(regexp, new_id));
    }
}

However this does not work, am i doing something wrong here. thanks for the help.

*EDIT
This is the form

<%= form_for @question, :url => { :controller => "questions", :action => "create" } do |f| %>
    <%= f.label(:name, "Request Question:") %>&nbsp;&nbsp;
    <%= f.text_field(:name, :size => 72, :maxlength => 120, :id => "name") %><br />
    <fieldset>
        <legend><b>Tags</b></legend>
        <%= f.fields_for :tags, :url => { :controller => "tags", :action => "create" } do |builder| %>
            <%= render "tag_fields", :f => builder %>
        <% end %>
        <p><%= link_to_add_fields "Add new keyword", f, :tags %></p>
    </fieldset>
<% end %>

The tag field partial

<p class="fields">
    <%= f.label(:keyword, "Keywords:") %>&nbsp;&nbsp;
    <%= f.text_field(:keyword, :size => 20, :maxlength => 25, :id => "keyword") %>
    <%= link_to_remove_fields "remove", f %>
</p>
  • 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-05-24T05:37:22+00:00Added an answer on May 24, 2026 at 5:37 am

    just a small change to calculate the amount of input fields:

    function add_fields(link, association, content) {
        if($(":input").length < 5) {
           // logic to add items
        }
    }
    

    If you want to check the input contents of a specific div with the id : “controls” (as example) :

    function add_fields(link, association, content) {
        if($("#controls :input").length < 5) {
           // logic to add items
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am following Ryan Bates' railcasts: http://railscasts.com/episodes/37-simple-search-form in solving my issue with the search
I am trying to use Ryan Bates 'letter-opener' gem as described in Episode #104
I have followed the omniauth devise facebook app as explained by Ryan in episode
I am trying to follow Ryan Bates screencast but have an error message. I
I'm using jquery_token_input after following Ryan Bates railscast #258 and this is the code
I have cloned a rails project from Ryan Bates (complex forms samples) and am
I am following Ryan Bates' railcast 146 and it is really helpful. However, Im
I've been following through Ryan Bates' railscast on using cancan but am stumped as
I have a form that works, but I need to have some fields hidden
I've been following Ryan Bates Railscast on Subdomains http://railscasts.com/episodes/221-subdomains-in-rails-3 and using lvh.me to serve

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.