On Sep 18, 10:49 am, mlittle <[email protected]> wrote:
> I have nested resources 2 levels deep and am having problems with the
> controller of the deepest resource;
>
> Parent > Child > Dog
>
> The rails guide says this for the forms, which seems to work fine:
>
> form_for [:parent, :child, @dog]
>
> I have all the appropriate belong_to and has_many but I cannot figure
> out how to do this in the dog controller:
>
> def create
> parent = Parent.find(params[:parent_id])
> child = Child.find(params[:child_id])
> @record= parent.child.dog.build(params[:dog])
> .....
> end
>
> I am getting this error: undefined method `child'

Not sure what the local variable "child" is doing?  It's not being
used in the 3rd line.

I would have expected the code to look like this:

parent = Parent.find(params[:parent_id])
child = parent.children.find(params[:child_id])
@dog = child.dogs.build(params[:dog])

This way, you're making sure that the child you find really belongs to
the parent.  Then, you build a new dog for that child's dog
collection.

Does this help?

Jeff
purpleworkshops.com

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to