我必须在Rails中重建一个看起来像这样的表单:
您正在考虑的是能够获得多年级(或学年)的学生,并且对于每个年级(或学年),您可以添加评估。有三个评估(开始,中间和最后),每个评估将有一定数量的科目。我得到他们所在的学校可以获得的科目,以便更新/改变价值,但我只希望在学校里教授这些科目。用户不应该能够"添加主题" ...我想像你在上面看到的那样定义它们。
我的问题是,我该如何实现这一目标?我有它的工作,所以我可以向学生添加无限数量的成绩(或学年),但我无法弄清楚如何无缝地添加另一个按评估分类的嵌套表格,所有科目都预设根据每次评估。模型如下:
Class Student < ApplicationRecord
belongs_to :school
has_many :student_grades, inverse_of: :student, dependent: :destroy
accepts_nested_attributes_for :student_grades, reject_if: :all_blank, allow_destroy: true
validates :status, presence: true
validates :school_id, presence: true
validates :first_name, presence: true
validates :middle_name, presence: false
validates :last_name, presence: true
enum :status => [:active, :applying, :graduated, :expelled]
end
class StudentGrade < ApplicationRecord
belongs_to :student
belongs_to :computer, optional: true
has_many :student_grade_subject_assessments, inverse_of: :student_grade, dependent: :destroy
accepts_nested_attributes_for :student_grade_subject_assessments, reject_if: :all_blank, allow_destroy: true
validates :student_id, presence: true
validates :school_year, presence: true
validates :grade, presence: true
validates :computer_id, presence: false
end
class StudentGradeSubjectAssessment < ApplicationRecord
belongs_to :student_grade
belongs_to :subject
validates :student_grade_id, presence: true
validates :subject_id, presence: true
validates :assessment_type, presence: true
validates :percentage, presence: false
validates :comments, presence: false
validates :plan, presence: false
enum :assessment_type => [:beginning_assessment, :mid_term_assessment, :final_assessment]
end
_form.html.erb
<%= form_for(@student, html: { multipart: true }) do |f| %>
<div class="tab-content">
<div class="tab-pane active" id="student-profile">
<div class="row">
<div class="col-xs-12 col-sm-4">
<div class="form-group">
<%= f.label :last_name, 'Last Name' %><br>
<%= f.text_field :last_name, class: 'form-control' %>
</div>
</div>
<div class="col-xs-12 col-sm-4">
<div class="form-group">
<%= f.label :first_name, 'First Name' %><br>
<%= f.text_field :first_name, class: 'form-control' %>
</div>
</div>
<div class="col-xs-12 col-sm-4">
<div class="form-group">
<%= f.label :middle_name, 'Middle Name' %><br>
<%= f.text_field :middle_name, class: 'form-control' %>
</div>
</div>
<div class="col-xs-12 col-sm-4">
<div class="form-group">
<%= f.label :school_id %><br>
<%= f.collection_select :school_id, School.all.order(:name), :id, :name, {prompt: "Choose..."}, {class: 'form-control'} %>
</div>
</div>
</div>
</div>
<div class="tab-pane" id="grades">
<%= f.fields_for :student_grades do |student_grade| %>
<%= render 'student_grade_fields', :f => student_grade %>
<% end %>
<div class='links'>
<%= link_to_add_association 'Add School Year', f, :student_grades, class: "btn btn-success" %>
</div>
</div>
</div>
<%= f.submit "Save Student", class: "btn btn-success" %>
<% end %>
_student_grade_fields.html.erb
只包含相应模型的字段,但我也在为StudentGradeSubjectAssessment
添加表单。
答案 0 :(得分:0)
看起来你想要这样的东西:
@student.student_grades.each do |grade|
...year, grade, computer stuff for the grade
grade.student_grade_subject_assessments.where(assessment_type: :beginning_assessment).each do |assessment|
...stuff for each beginning assessment per subject
end
grade.student_grade_subject_assessments.where(assessment_type: :middle_assessment).each do |assessment|
...stuff for each middle assessment per subject
end
grade.student_grade_subject_assessments.where(assessment_type: :end_assessment).each do |assessment|
...stuff for each final assessment per subject
end
end
您还可以通过按评估类型对一个查询进行分组,为每种评估类型保存多个查询:
grade.student_grade_subject_assessments.group_by(:assessment_type)
这将产生一个散列,其中键是评估类型,值是该评估类型的所有主题的数组(已按等级确定范围)。这是一个doc的例子。