Ruby on Rails - creating a dependent record with the parent's id

zefflyn

Registered. User.
Howdy all,

Anyone have tips or code snippets, or even the proper search terms, for how to code up in rails creating a dependent record, with the id of the parent?

For instance, two models: Parents, Children.

When looking at a Parent, I want to have a link to create a new Child, that is assigned to that Parent.

Only way I know how currently is to do new.Child, and in the form, have a field allowing choice of Parents.

Everything I've tried to link from a Parent record to a new Child record has failed:

<% link_to 'Add a kid', new_child_url(@parent.id) %>

And even though I've used @parent.id earlier in the form and printed the value, it fails saying "nil value", or complains that 'new_child_url' isn't a valid method.

Gah!
 

zefflyn

Registered. User.
Thanks crow!

That solved a couple other problems I've had, but they did the easy way of having the New Recipe form include a Categories drop-down list.

I want to go the other way, so that if I'm looking at a category, I click a link "Add recipe to this category" and it takes me to the new recipe form with the category_id already in the recipe form (or in a hidden field).

I'm doing a sort of inventory app to keep track of my stuff, so I have tons of items. I want to be able to add a "movement" to an item, but the list of items is WAY too long for a drop-down, so I'm trying to pass it from the item view to the movement new screen.
 
Top