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

  • Home
  • SEARCH
  • 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 7649167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T10:53:43+00:00 2026-05-31T10:53:43+00:00

I am having trouble with my nested form. I am using Ryan Bates’ code.

  • 0

I am having trouble with my nested form. I am using Ryan Bates’ code. Gemfile snippet:

gem “nested_form”, :git => ‘https://github.com/ryanb/nested_form.git’

The problem is that when I try to use *link_to_add* I get a new item which looks normal, but it doesn’t work because it gets a new index of 0 instead of a ‘random’ index based on the current time that the nested_form.js is supposed to generate. Every item that I add gets the same 0 index. So of the effect is I cannot add a new item (or only the last new one is saved, depending on the original state of the model).

The behavior is the same with a new parent object and when editing a parent object.

I am pretty sure I am doing something wrong since I was able to do a simple new app that works. But I can’t figure out what is wrong with my real app.

The nested_form.js generated for my (working) test app and my (not working) real app are identical. So I don’t think there is a problem with a version difference.

I used Firebug to step through the code in nested_form.js but was not able to figure out why it was generating a 0 index there.

My parent model has this:

class Course < ActiveRecord::Base

accepts_nested_attributes_for :levels, :allow_destroy => true
has_many :levels, :dependent => :destroy, :order => :depth

My form is instantiated like this:

<%= nested_form_for(@course) do |f| %>

The part with the nested object is like this:

<%= f.fields_for :levels do |builder| %>
    <tr>
        <td><%= builder.text_field :name %></td>
        <td><%= image_tag(builder.object.icon) unless (builder.object.icon.nil?) %></td>
        <td><%= builder.link_to_remove "Remove" %></td>
    </tr>
<% end %>
   <tr><td><%= f.link_to_add "Add a level", :levels %></td></tr>

I don’t see any difference between my code and my working example (or other examples I’ve seen).

Does anyone have any ideas?

UPDATE:

After trying to debug this some more in Firebug I realized the ‘blueprint’ used by the addFields function in nested_form.js was being changed inside the addFields method and that’s why it wasn’t putting in the generated index (that block was being bypassed in my test app). So I overrode the block where this happens by changing:

if (context) {

to:

if (false)

This seems to fix my symptom from preliminary testing but I sense that in some contexts (NPI) this may have broken something else (maybe with multiple levels of nesting?).

  • 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-31T10:53:44+00:00Added an answer on May 31, 2026 at 10:53 am

    Although I feel somewhat silly I think this is something that might happen to someone else (and I did say in my question I was probably doing something ‘wrong’).

    What I was doing ‘wrong’ was I was using fieldsets to segregate areas of my form. And I was giving the fieldsets the class ‘fields’. Not good. Conflicts with classes of the form generator.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having trouble with and RSpec view test. I'm using nested resources and the
I'm building a recurring billing system and am having trouble with a nested form.
I'm having trouble figuring out the proper way to update nested data using Google
I'm having trouble generating a nested model form. Here are my models: class Workout
I'm having trouble redrawing my nested list with sencha touch 2. My code looks
My nested model form is now working, but I am having trouble displaying the
I am having trouble getting a nested objects properties. For the example I am
I'm having trouble implementing a nested class who's constructor is initialized with some of
I am having some trouble creating a nested association and I can't quite spot
Having trouble linking the Stomp.framework into an iPhone SDK application. http://code.google.com/p/stompframework/ I follow the

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.