views:

105

answers:

1

Am trying to simplify the create / edit view for a multi-model controller.

User can dymically add / remove child input fields from the form. (Have followed Eloy's complex form example)

I want to limit the user's ability to set certain attributes across multilpe children..

Suppose I have a child attribute with and I want the user to only input the date once.. eg the date will be the same across all children..

I would like present a single date entry box and then multiple Adults | Seniors boxes depending on the number of children the user wants to submit.

using accepts_nested_attributes_for my form is showing multiple date boxes..

(Since I want to retain the ability to do this as an admin I don't want to move the date attribute to the parent.)

How should I go about adapting the form without having to extend the controller logic too much?

+1  A: 

If you have business logic that states that all of your children models have the same date, then I see this working a couple different ways.

First of all, maybe you have your data in the wrong place. If the date is always going to be the same for all of the children models, then why not make it an attribute on your parent model instead? You can always use the delegate method in your children model to fetch the date from the parent.

Another way I see of handling this is with a virtual attribute and a callback on your parent model. Use attr_accessor to create a virtual attribute on your parent model. Then in your form, add a field for the date to the parent model with the name you used to define the attr_accessor. Finally, add in a before_save callback (or whichever callback is appropriate for your case) in the parent model that saves the date to all of the children.

Jared
Hi Jared, thanks for your suggestion. Followed your approach of using a virtual attribute on the parent model. Only needed to override the save method to set the values to the children..
Dom