Rails 4考勤系统

时间:2015-06-05 08:50:29

标签: ruby-on-rails ruby ruby-on-rails-4 model

嗨我实际上正在努力构建一个在rails 4上实现考勤模型的Rails 4应用程序,我发现在stackoverflow上可能有两个问题,但是他们在2012年发布了,当我尝试关注它们时它失败了。

这是我得到的最近的on stackoverflow

编辑:我已经看到了课堂,并列出了学生。并且可以将学生分配到课堂,但问题是让学生进入新的 attsheet 并将他们保存到出勤

这是我现在拥有的

# 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

我希望教室能够为每个学生创建一个新的出勤或查看出勤档案。

我现在可以在教室里做到这一点,但我仍然坚持要为控制器做什么,并查看

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

2 个答案:

答案 0 :(得分:1)

在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

和参加者的_form.html.erb,

<%= 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 %>

这将提交fotm以创建出勤行动

答案 1 :(得分:1)

我通过

找到了解决方案

我将attsheet更改为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

简单字段

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