How to create multiple records simultaneously with ActiveScaffold in ROR

I want to use ActiveScaffold to create assignment records for several students in one step. All entries will contain the same data, except student_id.

I was able to override the default form and replace the drop-down list for selecting a student name using the multiple-element selection window - this is what I want. However, this change was only cosmetic, since the base code only captures the first selected name from this field and creates one record.

Can someone suggest a good way to do this in a way that does not require me to decrypt and rewrite too much of the base ActiveScaffold code?


Update: I still have not found a good answer to this problem.

+4
source share
3 answers

I assume that you have defined your field for multiple selection by adding: multiple => true for the select_tag html options. Then in the controller you need to access the list of selected names, which you can do as follows:

params[:students].collect{|student| insert_student(student, params[:assignment_id]) } 

When the collection is applied to an array or enumeration, you can scroll through each element of this array and then do what you need with each student (in the example, to call the function to insert students). The collection returns an array with the results of code execution inside.

+1
source

if your settings have has_many :students or has_and_belongs_to_many :students , then you can change the identifier of the multi-select field to assign_student_ids [], and it should work.

0
source

I was mentioned in BatchCreate , an ActiveScaffold extension that looks like it can do the trick.

0
source

Source: https://habr.com/ru/post/1276672/


All Articles