$(function(){
  $("#all-students-table").on("change", ".teacher-drop-down", updateTableFromDropDown);

  $("#all-students-table").on("submit", ".add-form, .remove-form", addStudent)
});

var updateTableFromDropDown = function(){
  var $that = $(this);
  var studentUrl = $that.parent().attr('action');
  var selectedTeacherId = $that.val();
  var url = studentUrl + "/teachers/" + selectedTeacherId;
  $.ajax({
    method: "patch",
    url: url
  })
  .done(function(data){
    var IDOfRowToReplace = $(data).attr("id")
    $('#' + IDOfRowToReplace).replaceWith(data);
  });
}

var addStudent = function(event){
  event.preventDefault();
  var $that = $(this);
  $.ajax({
    url: $that.attr('action'),
    method: "patch"
  })
  .done(function(data){
    var IDOfRowToReplace = $(data).attr("id");
    console.log($("#all-students-table").parent().attr("id"))
    if ($("#all-students-table").parent().attr("id") != "teacher-profile-students-table") {
      $('#' + IDOfRowToReplace).replaceWith(data);
    } else {
      $('#' + IDOfRowToReplace).remove();
    }
  })
}

@@ -455,122 +455,31 @@
 one(
-function(data)%7B%0A    var IDOfRowToReplace = $(data).attr(%22id%22)%0A    $('#' + IDOfRowToReplace).replaceWith(data);%0A  %7D
+conditionallyReplaceRow
 );%0A%7D
@@ -638,16 +638,74 @@
   .done(
+conditionallyReplaceRow)%0A%7D%0A%0Avar conditionallyReplaceRow = 
 function
@@ -712,18 +712,16 @@
 (data)%7B%0A
-  
   var ID
@@ -761,72 +761,8 @@
 %22);%0A
-    console.log($(%22#all-students-table%22).parent().attr(%22id%22))%0A  
   if
@@ -847,26 +847,24 @@
 ble%22) %7B%0A    
-  
 $('#' + IDOf
@@ -896,18 +896,16 @@
 (data);%0A
-  
   %7D else
@@ -911,18 +911,16 @@
 e %7B%0A    
- 
- 
 $('#' + 
@@ -953,15 +953,8 @@
 ;%0A  
-  %7D%0A  %7D)
+%7D
 %0A%7D%0A

Fix row breakage in other ajax done