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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:08:05+00:00 2026-05-26T16:08:05+00:00

I have a nested attributes form with the following and am just learning how

  • 0

I have a nested attributes form with the following and am just learning how to use nested attributes. One problem I’m having is that the child_index values is not incrementing up. I’m getting 3 fields based upon the build in the controller but they all have 0 or 1 depending on what number is set to.

Any ideas on how to get this to increment?

# in controller: 3.times {@item.assets.build}
<% number = 1 %>
<div id='files'>
  <%= f.fields_for :assets, :child_index => number do |asset| %>
    <p>
    number:<%= number %><br />
    <%=asset.label :asset, "File ##{number += 1}" %>
    <%= asset.file_field :asset %>
   </p>
<% end %>
</div>
<%= f.submit %>

edit:
so all of them in html would have the form like:

item[assets_attributes][0][asset]

rather than the desired:

menu_item[assets_attributes][0][asset]
menu_item[assets_attributes][1][asset] 
menu_item[assets_attributes][2][asset]

edit #2:
so looking through the source code, I see the following and am wondering if rails is supposed to be doing some auto-swapping and possibly this isn’t happening;

<input id="item_assets_attributes_0_asset" name="item[assets_attributes][0][asset]" type="file" />
<input id="item_assets_attributes_0_id" name="item[assets_attributes][0][id]" type="hidden" value="1" />
  • 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-26T16:08:06+00:00Added an answer on May 26, 2026 at 4:08 pm

    Looking through the Rails source it’s clear that if you specify :child_index there will be no auto-increment. Whether or not that is the proper behavior is debatable. If you completely omit the :child_index when calling fields_for, you should get the indexes you desire.

    To get the correct label for each field you could use some JavaScript. If you don’t like that, you could set the file number as an attribute of the Asset class.

    class Asset < AR
      attr_accessor :file_number
    end
    
    # in controller: 3.times {|n| @item.assets.build(:file_number => n) }
    
    <div id='files'>
      <%= f.fields_for :assets do |asset| %>
        <p>
        <%=asset.label :asset, "File ##{asset.file_number}" %>
        <%= asset.file_field :asset %>
       </p>
    <% end %>
    </div>
    <%= f.submit %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I keep finding that if I have nested divs inside each other, and one
I have a nested function to show/hide paragraphs news-ticker-style. The problem is that when
I have a nested form with the following models: class Incident < ActiveRecord::Base has_many
I am following alloy-complex-form-examples from github trying to learn nested forms and I just
I have a nested model form that isn't functioning properly. The POST is to
Just wondering if there's a method in rails to identify if nested attributes have
I have a model with nested attributes : class Foo < ActiveRecord::Base has_many :bar
I have some nested tables that I want to hide/show upon a click on
I have a nested form in which I would like the form fields to
I'm building a one page form that will create a model and it's relations.

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.