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?
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
/newtriggerscreate).models/person.rb
models/foo.rb
controllers/people_controller.rb
views/people/new.html.erb
This seems to do the trick.