[英]Form creation for a HABTM association
Here's what I'm trying to accomplish.这就是我想要完成的事情。 I have courses that students register for.
我有学生注册的课程。 I want to track the advanced students in that class.
我想跟踪 class 中的高级学生。 I set up another model called course_results.
我设置了另一个名为 course_results 的 model。
class CourseAdvanceType < ActiveRecord::Base
belongs_to :course
has_and_belongs_to_many :students
end
#######
class Students < ActiveRecord::Base
has_many :courses
has_and_belongs_to_many :course_advance_types
end
#######
class Courses < ActiveRecord::Base
has_one :course_advance_type
has_many :students
end
Ideally I want a small form that allows a selection of advance type (academic, athletic) that will allow me to add students below it.理想情况下,我想要一个允许选择高级类型(学术、运动)的小表格,这样我就可以在它下面添加学生。 Each student would be recorded in the course_advance_types_students table that is created for the HABTM association.
每个学生都将记录在为 HABTM 关联创建的 course_advance_types_students 表中。
Something like this:像这样的东西:
<form>
<select>
<option>academic</option>
<option>athletic</option>
</select>
<div id="students">
<div>Student ID: <input type="text" /></div>
<div>Student ID: <input type="text" /></div>
<!-- This link would dynamically add another name field -->
<div><a href="[code]">Add Student</a></div>
</div>
<input type="submit>
</form>
What is the best way to accomplish this?实现这一目标的最佳方法是什么? I thought about using accepts_nested_attributes_for, but I don't know if that works because I don't want to set attributes for students, I want the student ID to be saved with the course_advance_type_id in the course_advance_types_students table so that I can pull a report of Courses that have advance students with the list of the advance type and the list of students.
我考虑过使用accepts_nested_attributes_for,但我不知道这是否有效,因为我不想为学生设置属性,我希望将学生ID与 course_advance_types_students 表中的 course_advance_type_id 一起保存,以便我可以提取报告具有高级学生的课程以及高级类型列表和学生列表。
If I need to clarify something or rethink my approach, please tell me.如果我需要澄清某些事情或重新考虑我的方法,请告诉我。
Thank you.谢谢你。
Check out formtastic: https://github.com/justinfrench/formtastic查看表格:https://github.com/justinfrench/formtastic
Much easier way to put a form together for a has_many/habtm relationship.为 has_many/habtm 关系组合一个表单要简单得多。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.