I have array of checkboxes from which I need to store data row by row in the database.
Scenario is if user mark checkboxes the database should store value of is_present check as true and the information of that respective user as well.
If user mark uncheck, the database should store value of is_present check as false and the information of that respective user as well.
My main issue is I am unable to post both true and false at a time. I am only having true in params but unable to get false and its respective user information.
Also I am unable to see true or false belongs to which user in params.
I am also trying fields_for with it.
also how I can store them as row by row in database from params?
So, final words are, I have attendance table in which in the start there are no records but I am showing student registrations from some other table and I wants to tick or untick users from registrations table and have this information store in attendance table.
If tick marked then is_present as true and information of that user, if tick unmarked then is_present as false and information of that user.
The checkboxes could be 100+ so I created array of checkboxes.
Here is my code:
<%= form_for ([:teacher, @teacher, @class_room, @subject, @lesson, @attendance]) do |f| %>
<table id class="table table-bordered table-sm">
<thead>
<tr>
<th width="30%">Mark Attendance</th>
<th width="30%">Student</th>
<th width="30%">Grades</th>
</tr>
</thead>
<tbody id="class_room_attendances">
<% @class_room_registrations.each do |class_room_registration| %>
<tr>
<td>
<%= f.check_box "is_present", {}, true, false %>
<label>Check / Uncheck To Mark Attendance</label>
</td>
<%= f.fields_for :class_room_registrations, class_room_registration do |class_room_registration_fields| %>
<%= class_room_registration_fields.hidden_field :user_id, value: class_room_registration.user_id %>
<%= class_room_registration_fields.hidden_field :student_id, value: class_room_registration.student_id %>
<% end %>
</tr>
<% end %>
</tbody>
</table>
<div class="form-group">
<%= submit_tag 'Mark Attendance', class: 'btn btn-lg btn-info' %>
</div>
<% end %>
Create code:
def create
@attendance = @lesson.attendances.new(attendance_params)
flash[:notice] = "Attendance marked successfully"
respond_to do |format|
if @attendance.save
format.html { redirect_to teacher_teacher_class_room_subject_lessons_path(@teacher, @class_room, @subject) }
format.json { head :no_content }
format.js
end
end
end
Params are:
{"utf8"=>"✓",
"authenticity_token"=>"dy3eO7sNEBM+NV0Wwu08l/cIfoJp2VRJr06VW9B75K8Ru8s+q367vna
PK+6sCOxN1ICfcUYhLUaZ71HRI71C5g==", "attendance"=>{"is_present"=>"true",
"class_room_registrations"=>{"user_id"=>"5", "student_id"=>"1"}},
"commit"=>"Mark Attendance", "controller"=>"teacher/attendances",
"action"=>"create", "teacher_id"=>"delfina-kozey", "class_room_id"=>"15",
"subject_id"=>"4", "lesson_id"=>"25"}
Generated HTML:
<form class="new_attendance" id="new_attendance" action="/teacher/delfina-kozey/class_rooms/15/subjects/4/lessons/25/attendances" accept-charset="UTF-8" method="post"><input name="utf8" type="hidden" value="✓"><input type="hidden" name="authenticity_token" value="E2hKfNCGUePkmp5TLfr43rYW/3RP6znEekYHjxs5dMJ1/l95wPX6Tqwg6KtDHygElZ4eh2ATQMtM58MF6P/Siw==">
<table id="" class="table table-bordered table-sm">
<thead>
<tr>
<th width="30%">Mark Attendance</th>
</tr>
</thead>
<tbody id="class_room_attendances">
<tr>
<td>
<input type="checkbox" value="true" checked="checked" name="attendance[is_present]" id="attendance_is_present">
<label>Check / Uncheck To Mark Attendance</label>
</td>
<input value="6" type="hidden" name="attendance[class_room_registrations][user_id]" id="attendance_class_room_registrations_user_id">
<input value="2" type="hidden" name="attendance[class_room_registrations][student_id]" id="attendance_class_room_registrations_student_id">
</tr>
<tr>
<td>
<input type="checkbox" value="true" checked="checked" name="attendance[is_present]" id="attendance_is_present">
<label>Check / Uncheck To Mark Attendance</label>
</td>
<input value="5" type="hidden" name="attendance[class_room_registrations][user_id]" id="attendance_class_room_registrations_user_id">
<input value="1" type="hidden" name="attendance[class_room_registrations][student_id]" id="attendance_class_room_registrations_student_id">
</tr>
</tbody>
</table>
<div class="form-group">
<input type="submit" name="commit" value="Mark Attendance" class="btn btn-lg btn-info">
</div>
</form>
Form Screenshot:
Aucun commentaire:
Enregistrer un commentaire