之前我问了一个关于建立一个出勤名单的问题,让学生和建立一个检查表来标记出勤率并提出了这个问题。
def new
@attendance_list = @classroom.attendance_lists.new
@attendance_list.attendances = @classroom.student_ids.map do |student_id|
@attendance_list.attendances.build(student_id: student_id)
end
end
def create
@attendance_list = @classroom.attendance_lists.new(attendance_list_params)
@attendance_list.classroom_id = params[:classroom_id]
respond_to do |format|
end
PARAMS
params.require(:attendance_list)
.permit(:post_date, :remark,
attendances_attributes: [:student_id, :attended, :remarks ])
简单字段
= simple_form_for [@school, @classroom, @attendance_list] do |f|
= f.input :post_date
= f.input :remark
= f.simple_fields_for :attendances do |g|
** you see i needed a hidden student_id **
= g.input :student_id, as: :hidden
......
模型
class AttendanceList < ActiveRecord::Base
belongs_to :classroom
has_many :attendances
has_many :students, :through => :attendances
accepts_nested_attributes_for :attendances
end
class Attendance < ActiveRecord::Base
belongs_to :student
belongs_to :attendance_list
end
class Classroom < ActiveRecord::Base
belongs_to :school
has_and_belongs_to_many :students
has_many :attendance_lists
validates :class_name, presence: true
end
如果没有隐藏的输入,我该怎么办,因为这条线似乎不起作用。
build(student_id: student_id)
答案 0 :(得分:0)
class AttendanceList < ActiveRecord::Base
belongs_to :classroom
has_many :attendances
has_many :students, :through => :attendances
accepts_nested_attributes_for :attendances
end
class Attendance < ActiveRecord::Base
belongs_to :student
belongs_to :attendance_list
end
class Classroom < ActiveRecord::Base
belongs_to :school
has_and_belongs_to_many :students
has_many :attendance_lists
validates :class_name, presence: true
end
class Student < ActiveRecord::Base
has_many :attendances
has_many :attendance_lists, :through => :attendances
end
查看文件
= simple_form_for @student do |f|
= f.simple_fields_for :attendances do |g|
** your code **