0

I have a Company model that has_many Address(es) and many Phone(s).

Address(es) belong_to Company as do Phone(s).

My problem is that I don't understand how to edit a Company's particular Adress and Phone.

In my edit action, I call up the specific record and assign it to an instance variable (i.e. @address = some scoped searched for the specific address I want), and then in my fields_for I references this child's attributes, ex:

<%= f.fields_for :addresses, @address do |address| %>

A) I'm not sure if this is the way to do it. The documentation on how to access a parent's specific child for editing is sparse.

B) While this works fine if the update succeeds, when it fails and I render :edit the view presents additional fields with the parent's current child (the one I specified in my edit action + another child -- seemingly the next record in line).

So basically, my form is extended with two children when the render :edit is called. Weird.

What's the deal with this? How do nested attributes work? Is there a better way to manage forms with multiple associated models?

Thanks.

Nathan
  • 7,627
  • 11
  • 46
  • 80
  • I'm not sure I'm following exactly. When you say you are trying to edit the address, are you doing so through the form that also contains the parent or just trying to edit an address record in its own form? I'd think the former but your explanation sounds like the latter. – miked Apr 20 '12 at 14:16
  • Yes, when I say edit, I'm trying to edit the entire nested set. See the comments I left for dmarucco. – Nathan Apr 20 '12 at 19:40
  • Actually, after giving your comment some thought (and a bit more research on the fields_for method), I realized that fields_for loops through each record in the association. I was trying to select only a single record -- not the entire set that fields_for was looping through. But as I gave your comment more thought, I actually want the loop. If I want to only edit a single address instance, I can do a separate form for that. But as a nested record, I want all of the children. I just didn't understand that at first. Too bad you didn't post this as an answer ;-) – Nathan Apr 20 '12 at 22:06
  • Yeah, I was wondering if you were just using the fields_for outside of its intended purpose, that's why I asked :) I'll add it as an answer, if you want to accept, have at it. – miked Apr 23 '12 at 01:43

3 Answers3

1

It sounds like you are using the fields_for helper outside of its purpose here. From what you've described, you want to edit an address outside of its parents relationship. If that's the case you'll edit that address in its own form (and likely its own controller) using the form_for helper.

The fields_for would be used if you wanted to do any CRUD operations in the same form as the parent thereby leveraging the accepts_nested_attributes_for functionality.

miked
  • 4,489
  • 1
  • 17
  • 18
0

Defiantly watch the screen casts that dmarucco posted to get a good and general understanding of nested forms, then check out the problem I had before here with nested forms (I too had a many to many relationship):

Rails 3.1+ Nested Forms Issue: Can't mass-assign protected attributes

I posted my solution at the bottom of that page.

I also used nested_form and simple_form gems for formatting my nested forms, so you may want to look into that

Hope this sets you in the right path!

Community
  • 1
  • 1
mrcolombo
  • 587
  • 4
  • 19
-1

These screencasts could you help much more than thousands words.

dmarucco
  • 590
  • 4
  • 11
  • These are great, but they don't show how to edit a record. My problem is not creating complex forms, but editing an existing nested set. So if I have company > address, then I create an entry, I now have a record in the comany table and one in its associated address table. Then if I later add another entry (creating a second record for company and a second record for address), how do I pass parameters to the form such that I can choose which of these to edit? – Nathan Apr 20 '12 at 19:37