One Form for Creating Multiple Records under One Model

20 views
Skip to first unread message

Julian Medina

unread,
Jun 28, 2018, 1:37:54 AM6/28/18
to Ruby on Rails: Talk
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.

Colin Law

unread,
Jun 28, 2018, 3:18:44 AM6/28/18
to Ruby on Rails: Talk
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

Walter Lee Davis

unread,
Jun 28, 2018, 8:01:41 AM6/28/18
to rubyonra...@googlegroups.com
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
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLtz4W9oRvtUiKkdx8cMPnV1zqLN%3DpkOgUMQveTbY4JZYw%40mail.gmail.com.
> For more options, visit https://groups.google.com/d/optout.

Julian Medina

unread,
Jun 28, 2018, 4:49:20 PM6/28/18
to Ruby on Rails: Talk
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?

Julian Medina

unread,
Jun 28, 2018, 4:51:42 PM6/28/18
to Ruby on Rails: Talk
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.

Colin Law

unread,
Jun 29, 2018, 3:32:52 AM6/29/18
to Ruby on Rails: Talk
On 28 June 2018 at 21:51, Julian Medina <julian....@gmail.com> wrote:
> 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

Colin Law

unread,
Jun 29, 2018, 3:33:15 AM6/29/18
to Ruby on Rails: Talk
On 28 June 2018 at 21:49, Julian Medina <julian....@gmail.com> wrote:
> 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?

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

Julian Medina

unread,
Jul 6, 2018, 8:25:20 PM7/6/18
to Ruby on Rails: Talk
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.
Reply all
Reply to author
Forward
0 new messages