Submit_tag not submitting selected ids of the checkbox_tag.
here is the code :
<%= form_tag pattendance_attendances_path, method: :put do %>
<% if coordinator_signed_in? || admin_signed_in? %>
<ul class="list-group">
<li class="list-group-item">
<%= radio_button_tag "round", 1 , true %> Round 1</li>
<li class="list-group-item">
<%= radio_button_tag "round", 2 %> Round 2 </li>
<li class="list-group-item">
<%= radio_button_tag "round", 3 %> Round 3 </li>
<li class="list-group-item">
<%= radio_button_tag "round", 4 %> Round 4 </li>
<li class="list-group-item">
<%= radio_button_tag "round", 5 %> Round 5 </li>
</li>
<li class="list-group-item">
<%= radio_button_tag "mark_present", "present" , true %> Present <br>
<%= radio_button_tag "mark_present", "absent" %>Absent
</li><br>
<li class="list-group-item">
<%= submit_tag "Mark Attendance"%></li>
</ul>
<% end %>
</div>
<div class="table-responsive">
<div class="col-md-8">
<table class="table table-hover">
<thead>
<tr>
<th>Mark</th>
<th><%= model_class.human_attribute_name(:team_id) %> ID</th>
<th><%= model_class.human_attribute_name(:event_id) %> Name</th>
<th><%= model_class.human_attribute_name(:round) %></th>
<th><%= model_class.human_attribute_name(:status) %></th>
<th><%=t '.actions', :default => t("helpers.actions") %></th>
</tr>
</thead>
<tbody>
<% @attendances.each do |attendance| %>
<tr>
<td><%= check_box_tag "a_ids[]", attendance.id %></td>
<td><%= attendance.team_id %></td>
<td><%= attendance.event_id %></td>
<td><%= attendance.round %></td>
<td><%= attendance.status %></td>
<td>
<%if admin_signed_in? %>
<%= link_to t('.edit', :default => t("helpers.links.edit")),
edit_attendance_path(attendance), :class => 'btn btn-default btn-xs' %>
<%= link_to t('.destroy', :default => t("helpers.links.destroy")),
attendance_path(attendance),
:method => :delete,
:data => { :confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')) },
:class => 'btn btn-xs btn-danger' %>
<%end%>
<%= link_to t('.show', :default => t("helpers.links.View details")),
attendance, :class => 'btn btn-default btn-xs' %>
</td>
</tr>
<% end %>
</tbody>
</table>
</div>
</div>
<% end %>
Controller Code:
def pattendance
params[:a_ids]
if params[:mark_present]=="present"
Attendance.where(id: params[:a_ids]).update_all(status: 'Present', round: params[:round])
else
Attendance.where(id: params[:a_ids]).update_all(status: 'Absent', round: params[:round])
end
redirect_to attendances_url
end
The motive behind implementing this method is to mark attendance of all the teams registered in the particular event. The checkboxes are present to select multiple teams. I am selecting "Attendance.ids" because a team can participate in multiple events. So selecting Team IDS can mark attendance for the team in all the events. To make it unique for each event I'm using attendance ids.
After selecting rounds from radio buttons and status(present or absent) and clicking on mark attendance submit_tag it should update the values of the selected checkboxes. BUT Sometimes values are passed and sometimes they are not.
Please tell me what is the problem in this code. It would be really helpful.
Console : I tried to update the round of selected checkbox to "2" and status as "present"
Parameters: {"utf8"=>"✓", "authenticity_token"=>"2uzivV42tLjwuUd+QyEHvL6tCa8ZCE8xRbgJ5k7ueEcV9oaQt5yLafKmZTiFceZEY9B3wyY52qxL1f0hvqQ47A==", "round"=>"2", "mark_present"=>"present", "commit"=>"Mark Attendance"}
Coordinator Load (0.1ms)
SELECT "coordinators".* FROM "coordinators" WHERE "coordinators"."id" = ? ORDER BY "coordinators"."id" ASC LIMIT 1 [["id", 1]] SQL (0.2ms)
UPDATE "attendances" SET "status" = 'Present', "round" = 1 WHERE "attendances"."id" IS NULL Attendance Load (0.1ms) SELECT "attendances".* FROM "attendances" WHERE "attendances"."id" IS NULL Redirected to http://localhost:3000/attendances Completed 302 Found in 5ms (ActiveRecord: 0.4ms)
It didn't pass the selected checkbox values after clicking on submit button. The values don't update when I go back from show page to index page. But after refreshing it works consistently