Rails 4 Attendance System

Hi I am really trying to create a Rails 4 application that implements the attendance model on rails 4, I found maybe two questions on stackoverflow but they are posted in 2012 and it failed when I tried to follow them.

This is the closest I've gotten on StackOverflow Question / 2231791 / ...

Edit: I already have a class view and a list of students. And can assign students to class, but the problem would be to RECEIVE students in a new attsheet and keep them in attendance

Here I 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 new attendances or view attendance archives for each student.

I can do this in the class now, but I am stuck on what to do next for the controller and view

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

      

+3


source to share


2 answers


I found a solution by doing

I changed atthesheet 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 ...

      

+1


source


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 provide fotm to create attendance action

+1


source







All Articles