嗨,我正在努力构建一个rails 4应用程序,在rails4上实现一个考勤模型,我在stackoverflow上发现了两个问题,但是它们发布在2012年,当我尝试跟踪它们时失败了。
这是我最近得到的on stackoverflow
编辑:我已经看到了教室,列出了学生名单。可以把学生分配到教室里,但问题是让学生进入一个新的课堂,并让他们参加
这是我现在拥有的
# 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)
最佳答案
在附件控制器中,
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
在attendachen的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以创建出勤行动