我得到了一个模型列表Students
和Register
一个名为StudentRegisters
.
ARegister
有很多学生,aStudent
可以属于很多Registers
。
最初我希望能够在register/1
orregister#show
等中标记学生在场/缺席,但是我创建了一个名为 的新页面registration
,例如可以通过转到来访问register/1/registration
,并且在 中看起来像这样register controller
:
def registration
@studentregister = StudentRegister.find_all_by_register_id(params[:id])
end
这是页面外观的屏幕截图:
计划是,学生刷卡,在文本字段中读取,然后在Student
表中查找值,即Student.find_by_card_id', and if the student exists and is present in the current table, check the
当前checkbox and display the current time in the
到达时间field, and finally save both changed attributes in the
StudentRegister 模型,以便保存他们的出勤率。如果用户不存在,则什么也不会发生,或者如果学生存在于数据库中但不在当前表中,则可能会显示一条消息,或者只是忽略它。
我已经玩了很多关于让表格显示和更新的事情,目前每一行本身就是一个表单,并且附加了一个隐藏的提交按钮,该按钮被复选框的onClick
功能激活。
自己看看:
<table border="1">
<%= text_field_tag 'Card_ID',nil, :autofocus => true %>
<tr><td>Present</td>
<td>University ID</td>
<td>First Name</td>
<td>Last Name</td>
<td>Time of Arrival</td>
</tr>
<tr>
<% @studentregister.each do |t| %>
<%= simple_form_for t do |streg| %>
<td><%= streg.check_box :present, :onChange => "submit()", :class => 'student_present'%>
<%= streg.submit :style => 'display: none' %></td>
<td><%= content_tag :span, Student.find(t.student_id).university_id, :id => "#{t.student_id}"%></td>
<td><%= streg.label Student.find(t.student_id).first_name %></td>
<td><%= streg.label Student.find(t.student_id).last_name %></td>
<td><%= content_tag :span, t.time_of_arrival, :id => "toa_#{t.student.id}" %> </td>
</tr>
<% end %>
<% end %>
</table>
我还尝试了显示数据,因此我没有使用 Simple_Form 的label
方法,而是content_tag
为每一行提供一个 ID(感谢之前回答的问题)。不确定这是否正确,但我的源代码如下所示:
<table border="1">
<input autofocus="autofocus" id="Card_ID" name="Card_ID" type="text" />
<tr><td>Present</td>
<td>University ID</td>
<td>First Name</td>
<td>Last Name</td>
<td>Time of Arrival</td>
</tr>
<tr>
<form accept-charset="UTF-8" action="/student_registers/14" class="simple_form edit_student_register" id="edit_student_register_14" method="post" novalidate="novalidate">
<div style="margin:0;padding:0;display:inline">
<input name="utf8" type="hidden" value="✓" /><input name="_method" type="hidden" value="put" />
<input name="authenticity_token" type="hidden" value="c8B0JGs4ZLVILa4a4r4PJJrD/zFcLCDhTLscBKHbFeE=" /></div>
<td><input name="student_register[present]" type="hidden" value="0" />
<input class="student_present" id="student_register_present" name="student_register[present]" onChange="submit()" type="checkbox" value="1" />
<input name="commit" style="display: none" type="submit" value="Update Student register" /></td>
<td><span id="3">w122341434</span></td>
<td><label class="string optional control-label" for="student_register_Michael">Michael</label></td>
<td><label class="string optional control-label" for="student_register_Jones">Jones</label></td>
<td><span id="toa_3"></span> </td>
</tr></form>
<form accept-charset="UTF-8" action="/student_registers/15" class="simple_form edit_student_register" id="edit_student_register_15" method="post" novalidate="novalidate">
<div style="margin:0;padding:0;display:inline">
<input name="utf8" type="hidden" value="✓" /><input name="_method" type="hidden" value="put" />
<input name="authenticity_token" type="hidden" value="c8B0JGs4ZLVILa4a4r4PJJrD/zFcLCDhTLscBKHbFeE=" /></div>
<td><input name="student_register[present]" type="hidden" value="0" />
<input class="student_present" id="student_register_present" name="student_register[present]" onChange="submit()" type="checkbox" value="1" />
<input name="commit" style="display: none" type="submit" value="Update Student register" /></td>
<td><span id="7">w678941512</span></td>
<td><label class="string optional control-label" for="student_register_Stewie">Stewie</label></td>
<td><label class="string optional control-label" for="student_register_Lee">Lee</label></td>
<td><span id="toa_7"></span> </td>
我还创建了一些 JQuery 来测试它是如何工作的,但是每当我选中一个复选框时,它就不会提交表单,并且每当我有时间保存在Time of Arrival
字段中时,然后提交表单,它不存储那个时间。这是为什么?
<script>
$(function() {
$("input[name=Card_ID]").keydown(function(e) {
var id;
if (e.which === 13) {
id = this.value;
alert("That"); //Just to see that it works
$('#toa_'+id).text('2001-01-01 14:13:46 UTC');
//Sets the time for the row which as the toa_id that was entered in the checkbox
(Remember this is just a test to see how it works, in reality the Student ID should be checked, not the form ID)
$('form#edit_student_register_14 input[type="checkbox"]').attr('checked', true);
alert("This");
$('form#edit_student_register_14').submit();
}
})});