Rails 4 Attendance system

1.9k views Asked by At

Hi I'm actually struggling with building a Rails 4 application implementing an attendance model on rails 4, I found maybe two question on stackoverflow but they're posted in 2012 and it failed when i tried following them.

This is the closest one i got on stackoverflow

Edit: I already have a view of classroom and list out the students. And could assign students into the classroom but the problem would be to GET the students into a new attsheet and saving them into attendance

Here's what I currently have now

# Attendance
# take student as a single entry for :attsheet and
# has a :attended (boolean) and remarks as well
class Attendance < ActiveRecord::Base
  belongs_to :student
  belongs_to :attsheet
end    

#Attsheet which means attendance sheet
#has :post_date and :remark 
class Attsheet < ActiveRecord::Base
  belongs_to :classroom
  has_many :attendances
  accepts_nested_attributes_for :attendances 
end

class Student < ActiveRecord::Base
  belongs_to :school
  has_and_belongs_to_many :classrooms
  has_many :attendances
end

class Classroom < ActiveRecord::Base
  belongs_to :school
  has_and_belongs_to_many :students
  has_many :attsheets

  validates :class_name, presence: true
end

I want the classroom to be able to create a new attendance or view attendance archives for each student.

I am able to do this in classroom right now but I'm stuck at what to do next for the controller and view

 $ = link_to "New Attendance", new_school_classroom_attsheet_path(@school, @classroom, @attsheet) 
2

There are 2 answers

0
Chong Hui On BEST ANSWER

I found the solution by doing

I changed attsheet to attendance_list

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|
      if @attendance_list.save
        format.html {redirect_to school_classroom_path(@school, @classroom), notice: "You added the attendance!" }
      else

      redirect_to new_school_attendance_list_path(attendance_list_params)
      end
   end
end

with simple fields

= f.simple_fields_for :attendances do |g|
  = g.input :student_id, as: :hidden
  ...... more fields ...
2
bunty On

In attandances_controller,

class AttendancesController < ApplicationController

    before_filter :set_parents

    def new
        @attendance= @classroom.attendances.new
    end

    def create
        @attendance= @classroom.attendances.new(params[:milestone]) 

        if @attendance.save
            redirect_to ....
        else
            render :action=>:new
        end 
    end

   def set_parents
        @school= School.find(params[:school_id])
        @classroom= @school.classrooms.find(params[:classroom_id])  
   end
end

and in _form.html.erb of attendacen,

<%= form_for(@school, @classroom, @attendance]) do |f|%>
<% if @attendance.errors.present? %>
<ul class="warning">
    <% @attendance.errors.full_messages.each do |message| %>
    <li><%= message%></li>
    <% end %>
</ul>
<% end %>

<h2>Attendance</h2>
.........
<%= f.submit button %>
<% end %>

this will submit fotm to create action of attendance