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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:59:46+00:00 2026-05-13T12:59:46+00:00

Thanks to Ruby on Rails: How to gather values for child tables from a

  • 0

Thanks to Ruby on Rails: How to gather values for child tables from a form? and “Agile Web Dev”, I know how to have multiple models in a form using fields_for. But I’m tearing my hair out over this one.

Suppose I have a model Person. Person has a name attribute, and has_many :foos. The Foo model, in turn, has a colour attribute.

Furthermore, I know that each Person has precisely three Foos. What should my Models, the new and create actions in PersonController, and the new view look like in order to present three nicely-labelled text-entry boxes, one for each Foo and capable of reporting validation errors, to allow my “new person” form to create the whole set of four objects in one go?

Also, can I do this without accepts_nested_attributes_for?

  • 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-13T12:59:46+00:00Added an answer on May 13, 2026 at 12:59 pm

    After some playing about with varied locations for square braces and different for loops, I think I’ve solved this. Here’s what my code looks like now (with routes set up as per scaffolding, so that posting from /new triggers create).

    models/person.rb

    class Person < ActiveRecord::Base
      has_many :foos
      validates_presence_of :name
    end
    

    models/foo.rb

    class Foo < ActiveRecord::Base
      belongs_to :person
      validates_presence_of :colour
      validates_uniqueness_of :colour, :scope => "person_id"
    end
    

    controllers/people_controller.rb

    def new
      # Set up a Person with 3 defaulted Foos
      @person = Person.new
      (1..3).each { |i| @person.foos.build }
    end
    
    def create
      # Create (but don't save) a Person as specified
      @person = Person.new(params[:person])
    
      # Create (but don't save) a Foo for each set of Foo details
      @foos = []
      params[:foo].each do |foo_param|
        @foos << Foo.new(foo_param)
      end
    
      # Save everything in a transaction
      Person.transaction do
        @person.save!
        @foos.each do |foo|
          foo.person = @person
          foo.save!
        end
      end
    
      redirect_to :action => 'show', :id => @person
    
    rescue ActiveRecord::RecordInvalid => e
      @foos.each do |foo|
        foo.valid?
      end
      render :action => 'new'
    end
    

    views/people/new.html.erb

    <% form_for :person do |f| %>
      <%= error_messages_for :object => [@person] + @person.foos %>
    
      <p>
        <%= f.label :name %><br />
        <%= f.text_field :name %>
      </p>
    
      <table>
      <% @person.foos.each_with_index do |foo, index| @foo = foo%>
        <tr>
          <td><%= label :colour, "Foo colour #{index + 1}: " %></td>
          <td><%= text_field("foo[]", "colour" %></td>
        </tr>          
      <% end %>
      </table>
    
      <p>
        <%= f.submit 'Create' %>
      </p>
    <% end %>
    

    This seems to do the trick.

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

Sidebar

Related Questions

No related questions found

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.