【问题标题】:Rails 4 Attendance systemRails 4考勤系统
【发布时间】:2015-06-05 08:50:28
【问题描述】:

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

这是我得到的最接近的on stackoverflow

编辑:我已经有了教室视图并列出了学生。并且可以将学生分配到教室,但问题是让学生进入一个新的 attsheet 并将他们保存到 attendance

这是我现在拥有的

# 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) 

【问题讨论】:

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


    【解决方案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 以创建出勤操作

    【讨论】:

    • 你可以参考this,这是rails的基本应用。虽然它在 rails-3.2 中。您仍然可以使用它来构建 rails-4 应用程序。
    • 嗨!感谢您的回复,但我有一个学生教室,我希望出勤人员列出当前学生并保存。这就是为什么我使用考勤表来跟踪日期。在 attsheet 中对 @classtoom.students.each 做的事情
    【解决方案2】:

    我通过这样做找到了解决方案

    我将 attsheet 更改为出席名单

    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 ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-17
      • 1970-01-01
      • 2019-10-12
      • 2020-11-11
      • 2021-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多