Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何实现AJAX来删除条目?_Ruby On Rails_Ajax - Fatal编程技术网

Ruby on rails 如何实现AJAX来删除条目?

Ruby on rails 如何实现AJAX来删除条目?,ruby-on-rails,ajax,Ruby On Rails,Ajax,我正在尝试使用AJAX从Rails应用程序中删除一个条目。我是阿贾克斯的新手,我有点被卡住了,因为两件事 我不认为我的url字段是否正确。我从jquery中使用$this获取一个特定条目,但我不确定是否对task_path(变量)使用“this”。我需要通过一个特定的任务进行删除,但这是实现这一点的正确方法吗 我没有使用任何需要的东西。currentTarget();通常在我看到的所有AJAX示例中,.currentTarget()用于从表单获取信息,然后在.AJAX调用中进行操作。我没有表格。

我正在尝试使用AJAX从Rails应用程序中删除一个条目。我是阿贾克斯的新手,我有点被卡住了,因为两件事

  • 我不认为我的url字段是否正确。我从jquery中使用$this获取一个特定条目,但我不确定是否对task_path(变量)使用“this”。我需要通过一个特定的任务进行删除,但这是实现这一点的正确方法吗

  • 我没有使用任何需要的东西。currentTarget();通常在我看到的所有AJAX示例中,.currentTarget()用于从表单获取信息,然后在.AJAX调用中进行操作。我没有表格。我是否需要为删除操作实现.currentTarget

  • 这是我的ajax.js文件:

    $(function() {
    $(this).on('delete', function(event) {
    event.preventDefault();
    
      $.ajax({
        type: 'DELETE',
        url: task_path(@task),
        dataType: 'json',
        success: function(response) {
          $(this).remove();
        },
      });
     });
    });
    
    def destroy
      @task = Task.find(params[:id])
      @task.destroy
      respond_to do |format|
      format.html { redirect_to root_path, notice: "Task Successfully Removed, Good job!"}
      format.json { render root_path, notice: "Task Successfully Removed, Good job!" }
      end
    end
    
    这真的让我绞尽脑汁。谢谢你的帮助

    ---------------编辑----------------

    我试着做了推荐的更改,并提出了这组代码。我想我已经接近了,但我还没有完全达到

    $(function() {
    $(this).on('delete', function(event) {
    event.preventDefault();
    
    var id = $(this).attr("href");
    var data_info = $(this);
    
      $.ajax({
        type: 'DELETE',
        url: id + "destroy",
        dataType: 'json',
        data: data_info,
        success: function(response) {
          $(this).remove();
        },
      });
      });
    });
    

    看起来您在
    url
    字段中混合了客户端和服务器端逻辑。如果我了解您的情况,您正在尝试使用
    url:task\u path(@task)
    运行该.js文件。由于该文件位于浏览器中,它不知道如何将
    task\u path
    转换为实际路径。你也需要这样做

  • 使用实际路径('/tasks/$(this.id)/destroy)和一些获取URL正确id的方法(例如,可以使用HTML中的数据属性)
  • 将文件转换为.js.haml文件,并在需要时呈现/提供它。这不是最佳做法

  • 除此之外,ajax调用看起来像是正确的jQuery。

    您应该将
    id
    存储在
    数据-
    属性中,而不是
    href
    中。例如,您可以有
    数据id=
    (假设您使用的是
    erb
    )。然后获取
    id
    ,并在
    js
    文件中使用它

    对于
    url
    ,您可以使用,它提供Javascript中的Rails路由。因此,您的Javascript应该是这样的:

    $(function() {
        $(this).on('delete', function(event) {
            event.preventDefault();
    
        var id = $(this).attr("data-id");
        var data_info = $(this);
    
        $.ajax({
          type: 'DELETE',
          url: Routes.task_path(id),
          dataType: 'json',
          data: data_info,
          success: function(response) {
            $(this).remove();
          },
        });
      });
    });
    

    这应该给你一个线索。

    Hey@ABMagil,谢谢你的回复,你能看看我的编辑吗?这就是我想要得到的。我怎样才能了解我的变量是什么?我尝试使用调试器;但它似乎并没有停止。这取决于你点击的内容。我的建议是查看Chrome开发工具-右键单击您正在单击的任何元素以引起AJAX操作,并使用“Inspect Element”查看链接/按钮/您正在单击的任何元素上的属性。看看这个开发工具,你会发现我在按钮中添加了一个数据属性。单击它们时,可以使用该数据属性控制AJAX请求中的“url”字段。谢谢Ako。我感谢你在这方面的帮助。