【发布时间】:2018-03-31 22:00:39
【问题描述】:
我必须在 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 放置表单的地方。
【问题讨论】:
标签: ruby-on-rails nested-forms