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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T18:51:23+00:00 2026-06-05T18:51:23+00:00

I have two model classes, say parent and child. Parents are created first, and

  • 0

I have two model classes, say parent and child. Parents are created first, and then a child can be added.

I’m trying to have an edit page where you can edit the information on for the parent and possibly add a child.

I have it set so that the parents can access child attributes:

has_one :child
accepts_nested_attributes_for :child

And in my view to edit parent information, I have a form that shows all the parent information, and then fields to either edit existing child information or add a new child:

<%= form_for(@parent, :as => :parent, ...  %>       
    <%= form.text_field(:field_one) %>
    <%= form.text_field(:field_two) %>

    <%= form.fields_for @child do |child| %>  
        <%= child.text_field(:child_field_one) %>
        <%= child.text_field(:child_field_two) %>
    <%end%>
<%end%>

The problem is that the only way I can get this to work is to have my parent edit controller create a new child and associate it with the parent before rendering the view. Then when the user tries to save, my controller checks to see if the child fields are empty and deletes the newly created child if it is. One problem with this is that it creates an empty child (assuming a child doesn’t exist) every time the edit page is loaded, and then saves it to the database (which I don’t want)

Is there an elegant way of showing the fields for a non-existent child on the parent edit page without creating a new child before rendering? Ideally I want everything on a single form. I’m pretty sure I can come up with a way of doing this in multiple forms with multiple save buttons.

  • 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-05T18:51:25+00:00Added an answer on June 5, 2026 at 6:51 pm

    Maybe I don’t fully understand what you’re trying to do, but if you are using a fields_for tag, you need an object. The only other way to do that is to use the symbol and call it as:

    <%=form.fields_for :child do |child| %>
    

    But (assuming that’s even still valid in Rails 3) that’s effectively equivalent to

    <%=form.fields_for Child.new do |child| %>
    

    which is the same as

    @child = Child.new
    

    In the controller action.

    I’m pretty sure that, in order to use the form helper, you need an object to iterate over.

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

Sidebar

Related Questions

Let's say I have two classes, Parent and Child. These two are related via
Say I have two classes: class A(db.Model): class B(db.Model): a_reference = ReferenceProperty(A) I can
I have a hibernate model with two classes, let's say A and B. B
Let's say I have the following two classes: package example.model; public class Model {
I have two questions. 1: At the moment I have two model classes that
Given two model classes, Foo and Bar , I want Foo to have 3
I have two model: User, Article A user can like or dislike many articles,
I have two tables related: DataEntered and Model DataEntered -currentModel Model One DataEntered can
I have two tables registries and names . Consequently, I have two model classes.
I have a two model classes which have relationship of one to many. public

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.