mercredi 27 juin 2018

Change status based on checkbox value rails

I have multiple checkboxes(which is for status of a record) in a page, I need to update the status of records while a button is clicked

<% collection.each do |staff| %>
  <div class="col-sm-6 col-md-4 col-lg-3" id="staff-<%= role.id %>">
 <input class="filled-in chk-col-indigo" type="checkbox" id="staffStatus- 
    <%=staff.id %>">
 <label class="labelValue" for="staffStatus-<%= staff.id %>">
<% end%>

when the submit button is clicked I just take all the checkbox based on checked status,

$(document).on 'click', '#StatusUpdateButton', ->
     inactive = []
     active = []
     $.each $(".staff__active:not(:checked)"), ->         
     inactive.push($(this).val())
     $.each $(".staff__active:checked"), ->
     active.push($(this).val())

In controller,

def update_role_status
             Staff.where('id IN (?)', params[:active]).update_all(active: true)
             Staff.where('id IN (?)', params[:inactive]).update_all(active: false)
        end

I am updating the status, But i want to do it with better option, Is that okay to send all the checkbox status or will that be good to send only changed check boxes, I am in confusion to take the better option, Any suggession




Aucun commentaire:

Enregistrer un commentaire