views:

993

answers:

4

I am wanting to use ActiveScaffold to create assignment records for several students in a single step. The records will all contain identical data, with the exception of the student_id.

I was able to override the default form and replace the dropdown box for selecting the student name with a multi-select box - which is what I want. That change however, was only cosmetic, as the underlying code only grabs the first selected name from that box, and creates a single record.

Can somebody suggest a good way to accomplish this in a way that doesn't require my deciphering and rewriting too much of the underlying ActiveScaffold code?


Update: I still haven't found a good answer to this problem.

A: 

if your assingnments have has_many :students or has_and_belongs_to_many :students, then you can change the id of the multi-select box to assignment_student_ids[], and it should work.

Can Berk Güder
My assignments records are one-record-per-student, as there are fields such as "notes" which are student-specific.If I understand your answer correctly, this will allow you to select multiple names on the form, but the ajax request will remain unchanged.
Brent
A: 

perhaps before_create_save can be used for this?? Does anyone know?

Brent
Apparently not - before_create_save is called with a single model instance.
Brent
A: 

I was referred to BatchCreate, an ActiveScaffold extension which looks like it might do the trick.

Brent
+1  A: 

I suppose you have defined your multi-select box adding :multiple => true to html parameters of select_tag. Then, in the controller, you need to access the list of names selected, what you can do like this:

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

With collect applied to an array or enum you can loop through each item of that array, and then do what you need with each student (in the example, to call a function for insert the students). Collect returns an array with the results of doing the code inside.

ARemesal