How to create multiple forms for Single Model and all forms must be submitted by one submit button?

StackOverflow https://stackoverflow.com/questions/17495869

Question

I need to Add multiple records of one Model on a single submit button.

For Example

I need user to create multiple tasks at the end of the day. Each of these tasks has few attributes and what I need is a "Add another task" link or button to add another task record.

In short User Experience is very Similar to Standard Nested Form procedure.

Task does not have any parent resource which I could use in real world.

Task belongs_to 'user' and 'project' but actually they are not parent to the Task as I am not creating any record of User or Project while creating Task.

I do not want to limit the recursion of the form controls with "10.times" etc. as that is not right approach.

Was it helpful?

Solution

If you want only a submit button, then it's only one form (this is how HTML forms work).

If I understood right your request, here is a suggestion:

  • use javascript on a button on the page, to create more and more tasks, with an incrementing parameter
  • the submit button should point to a controller's method which creates them all, identifying them by that incrementing parameter

Also, you could create the tasks using AJAX, but this means they will go to the database right away.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top