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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:10:19+00:00 2026-06-03T07:10:19+00:00

The fields_for block doesn’t output in a has_many relationship. This problem came up in

  • 0

The fields_for block doesn’t output in a has_many relationship. This problem came up in a somewhat involved project I was working on. I broke it down to a very simple test case, but it still doesn’t work. This question has been asked before and the problem was usually
that the nested object didn’t exist. But here, the nested object does appear to exist, as explained in the code comments. I’m pretty new to rails so it could be something obvious.

Here is the simple case model code:

class Parent < ActiveRecord::Base
    has_many :children
    accepts_nested_attributes_for :children
end

class Child < ActiveRecord::Base
    belongs_to :parent
end

Simple case controller:

class ParentController < ApplicationController
  def index
    @parent = Parent.find_by_id(1)
  end
end

Simple case view:

<%= form_for @parent, {:url=>{:action=>:index}} do |f| %>

    <!-- this outputs ok -->
    <%= f.text_field :name %>

    <% f.object.children.each do |c| %>

        <!-- this outputs "child1", so the nested object exists -->
        <%= c.name %> 

        <% f.fields_for c do |field| %>
            this line does NOT output, nor does the field below
            <%= field.text_field :name  %>
        <% end %>
    <% end %>
<% end %>

I also tried this and saw the same result:

<%= form_for @parent, {:url=>{:action=>:index}} do |f| %>
    <%= f.text_field :name %>
    output here     
    <% f.fields_for :children do |field| %>
        no output here nor the field below
        <%= field.text_field :name  %>
    <% end %>
<% end %>

I also tried with a new @parent object in the controller and a @parent.build_child
(having changed the assoc to a has_one). That still saw the same result.

  • 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-03T07:10:20+00:00Added an answer on June 3, 2026 at 7:10 am

    You’ve forgotten to put the = sign after <%.

    Replace:

       <% f.fields_for :children do |field| %>
    

    with:

       <%= f.fields_for :children do |field| %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Seems like the problem with this is the PHP syntax, but no luck in
First, this issue is not about block elements collapsing when their children are floated.
So I've been stuck on this problem for a day or so now and
I nominate me for village idiot. Why doesn't this work: foreach (XElement clientField in
I make this call in my application.js file but it doesn't seem to return
I have isolated the problem in the second block of code below (if you
Edit: This code is fine. I found a logic bug somewhere that doesn't exist
So I've got this helper method, right? def table_form_field(name_or_options = nil, *args, &block) #
I have a table called invoices with 3 fields (for the purposes of this
I've been searching the web for a way to do this for about a

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.