如何在 ROR 中使用 ActiveScaffold 一次创建多个记录

发布于 2024-07-05 06:04:20 字数 288 浏览 9 评论 0原文

我想使用 ActiveScaffold 一步为多个学生创建作业记录。 除 Student_id 外,所有记录都将包含相同的数据。

我能够覆盖默认表单,并用多选框替换用于选择学生姓名的下拉框 - 这就是我想要的。 然而,这种变化只是表面上的,因为底层代码仅从该框中获取第一个选定的名称,并创建一条记录。

有人可以建议一种不需要我破译和重写太多底层 ActiveScaffold 代码的好方法来完成此任务吗?


更新:我仍然没有找到这个问题的好的答案。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

混吃等死 2024-07-12 06:04:20

我想您已经定义了多选框添加:multiple => true 为 select_tag 的 html 参数。 然后,在控制器中,您需要访问所选名称列表,您可以执行以下操作:

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

将收集应用于数组或枚举,您可以循环遍历该数组的每个项目,然后对每个学生执行您需要的操作(在示例中,调用一个函数来插入学生)。 Collect 返回一个数组,其中包含执行内部代码的结果。

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.

—━☆沉默づ 2024-07-12 06:04:20

如果您的分配有 has_many :studentshas_and_belongs_to_many :students,那么您可以将多选框的 id 更改为分配_student_ids[],它应该可以工作。

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.

面犯桃花 2024-07-12 06:04:20

我被提到了 BatchCreate,一个 ActiveScaffold 扩展,看起来可能做这个伎俩。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文