rails: how to update a has_many :through relation via jQuery?

假如想象 提交于 2019-12-06 10:56:04

All you need is for the delete action to return a success or failure (even just true or false will do), then your javascript can deal with either response.

If a failure then return a message saying so, if a success and the relationship deleted, you could find the <tr> parent of the delete link that you clicked and fade the row out, eventually deleting it from the page. For example (given that your controller returns true if the delete is successful and false otherwise):

$('.edit_contact_join_delete').livequery('click', function() {
    var $deleteButton = $(this);
    var answer = confirm("Sure?");
    var dataloc = "&_method=delete";
    if (answer) {
     $.ajax({
          type : "POST",
          url  : this.href,
          data : dataloc,
          success: function(result) {
            if (result) {
              // handle successful delete
              $deleteButton.parent('tr').fadeOut().remove()
            } else {
              // handle failed delete
              alert('Delete failed');
            }
          } 
        });
    }
    return false;   
  });
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!