I know I’m close, but I’m stuck.
These are the three models I’m working with: AttendanceSheet, Attendance and Child.
AttendanceSheet
has_many :attendances, :dependent => :destroy
accepts_nested_attributes_for :attendances
belongs_to :course
Child
has_many :attendances
Attendance
belongs_to :attendance_sheet
belongs_to :child
So the join model is Attendance. I’m trying to create an attendance sheet with a list of all students from a particular course and then use a checkbox to mark if they attended or not. Like this…
Attendance Sheet
Course: Biology
Date: _____________
Michael Scott [] Notes: sick
Jim Halpert [] Notes: ____
Dwight Schrute [] Notes: ____
So the attendances tables has the following columns:
child_id
attended (boolean) to check if the student attended course or not
notes
The part I’m having trouble with is coming up with some kind of loop to display all the students that belongs to that class and having fields for attended and notes for each of them.
This is what I have…
_form.html.erb
<%= simple_form_for @attendance_sheet, :html => { :class => 'form-horizontal' } do |f| %>
<h2>Course: <%= @course.name %></h2>
<div class="form-inputs">
<%= f.input :attendance_on, :as => :string, :hint => 'YYYY-MM-DD', :input_html => {:class => :datepicker, :value => Date.today} %>
</div>
<% @course.children.each do |child| %>
*** trouble here ***
<%= check_box_tag %> <%= child.full_name %><br />
<% end %>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
attendance_sheets_controller.rb
def new
@attendance_sheet = AttendanceSheet.new
@course = Course.find(params[:course_id])
respond_to do |format|
format.html
end
end
Using rails
accepts_nested_attributes_for :attendances, you can do something like this in your controller:Then do something like this in your
simple_form_for @attendance_sheet