One Form for Creating Multiple Records under One Model

Hello:

I have an interesting yet somewhat strange task. Currently, there is a form in my app which adds just single record for certifying tutors. The group wants a way for volume purchasing where a coordinator can login and add multiple tutors in one form. I was considering a nested form or form object but since the certificate actually follows the individual instead of coordinator, I am now thinking it is best if multiple records under the “Certifcation” model get created under a single form. I really don’t see why I’d need an entirely separate model for this. Would I need to implement a “hacky” solution for this?

Any advice would be greatly appreciated.

Remember you can have multiple actions on a controller which bring up different forms. So you can have a separate action that brings up the multi-record form.

Colin

Also, if you wanted to stick to the REST actions in your controllers, you could have a separate controller (with no additional model): think TutorsController and MultipleTutorsController or something like that. Rails conditions you to think that a controller must be unique to its model, but controllers don't even need models if that's not what they are doing, and multiple controllers can help refine your routing around a single model -- think a ContentsController and an AdminContentsController, with the same Content model.

Walter

Thanks! So essentially I could have a separate button for volume purchases which would bring up a different view? Would I then have a loop in my controller that would create records for each person who is getting certified?

This is an interesting approach. Thanks! Would I essentially just duplicate my current controller but add a loop within the volume controller so that records are created based on the number of individuals getting certified? I am assuming I’d need two separate views as well.

Obviously you need a view to show your form. You shouldn't have to duplicate any actions from the current controller as presumably this controller will only have the one action in it - showing the form, adding the records, and handling any errors.

Colin

Since you haven't quoted the previous message we don't know who this is addressed to. Assuming it was my suggestion that you could have a different action in the same controller then presumably yes, that is what you would do. Of course the code can get tricky as you may have to allow for problems saving the record for each one (caused by validation errors) and work out how to handle that.

Colin

Sorry, I couldn’t find a quote button on the mobile version.

I have been thinking more about it. Perhaps I can simply rename my controller, model, and view to “Certification Application” or “Certification Request” then create a model for “Individual Name” and create a has_many relationship. Technically, a “Certification” cannot have many individual names. However, a “Certification Application” or “Certification Request” can easily have many individual names. I can then use the Cocoon gem and make a nested form. The biggest challenge I see is making each dynamically created field increase the quantity. Perhaps I should have hidden fields for quantity and price that can dynamically get adjusted. Then upon submission, those can be passed to the backend which will calculate the correct amount to send to Stripe. Does this sound like the right approach?

Thank you again for the assistance.