Suppose i have a class name Object, which has "Name" and "use" attributes. A second class named "Person" has an attribute called "how_many_objects_this_person_has". I'm trying to create a view that will look for a Person (i provide the id), see how many objects this person has, say N, and creates a form with N lines, one for each object, with fields for the two object's attributes. When the user cl开发者_如何学Cicks the submit button all objects must be saved, each one having an unique object id.
Any suggestion? I know it seems very simple, but i'm an starter. I looked for the answer for a whole week now and couldn't solve it myself.
Thanks a lot, Gustavo
http://railscasts.com/episodes/196-nested-model-form-part-1 http://railscasts.com/episodes/197-nested-model-form-part-2
I think you will find that for a decent user experience you will most probably wish to allow the user to add and remove records as they go along in addition to/instead of predetermining the number of records to add. The above railscasts show how to do this
Oh yeah! I used the same method in another part of my app. Didn't realize i could just do the same thing but without showing anything about the Person class.
By the way, there are two things to pay attention in that tutorial. You have to list the :objects_attributes in the attr_accessible (as said above). And the other is that there is a double html escape in the javascript code which makes the actual html to be rendered. Just look for some text in "h(text text text text)" and remove the "h()".
Thanks a lot!
精彩评论