简体   繁体   English

无法删除表中的项目 Ruby 在轨道上

[英]Cant delete item in table With Ruby on rails

Im trying to delete an item from a table i generate using a scaffold.我试图从我使用脚手架生成的表中删除一个项目。 I done some research and seen this is a common issue with windows machines where instead of deleting the item it just gets it.我做了一些研究,发现这是 windows 机器的常见问题,它不是删除项目而是直接获取它。 This is my destroy method in the controller这是我在 controller 中的销毁方法

 def destroy
    @measurment.destroy
    respond_to do |format|
      format.js
      format.html { redirect_to measurments_url, notice: "Measurment was successfully destroyed." }
      format.json { head :no_content }
    end
  end

This is the fix that I tried below.这是我在下面尝试的修复。 I added remote: true and class: "delete" to the destroy link in the index page我在索引页面的销毁链接中添加了 remote: true 和 class: "delete"

<td><%= link_to 'Destroy', measurment, method: :delete, data: { confirm: 'Are you sure?', remote: true }, class: "delete" %></td>
      </tr>
  

I then added jquery using yarn add jquery and added //= require jquery //= require rails-ujs to the application.js file.然后我使用纱线添加 jquery 添加 jquery 并添加 //= 需要 jquery //= 需要 rails-ujs 文件到 application.js Lastly i created a file in the measurements folder called destroy.js.erb.最后,我在测量文件夹中创建了一个名为 destroy.js.erb 的文件。 This contained:这包含:

$('.delete').bind('ajax:success', function(){
    $(this).closest('tr').fadeOut();
});

After all that i still cant delete items from the table.毕竟我仍然不能从表中删除项目。 Any help would be greatly appreciated任何帮助将不胜感激

Using Button_to instead of link_to works使用 Button_to 而不是 link_to 有效

If you're using Server-generated JavaScript Responses (SJR) you want to perform the actions directly instead of adding an event handler:如果您使用服务器生成的 JavaScript 响应 (SJR),您希望直接执行操作而不是添加事件处理程序:

<%= link_to 'Destroy', measurment, method: :delete, data: { confirm: 'Are you sure?', remote: true }, class: "delete", id: "measurment-#{measurment.id}" %>
$("#measurment-<%= @meaurement.id %>").closest('tr')
                                     .fadeOut();

The way that SJR works is that the return javascript is popped into a script tag and executed immediately after the request is completed. SJR 的工作方式是将返回的 javascript 弹出到脚本标签中,并在请求完成后立即执行。 However this code has absolutely no idea about which element fired the orginal event so you have to get hacky and unique ids.然而,这段代码完全不知道哪个元素触发了原始事件,所以你必须得到 hacky 和唯一的 id。

If you instead send a JSON request you can write a lot better javascript and not make your server responible for altering the page.如果您改为发送 JSON 请求,您可以编写更好的 javascript 并且不让您的服务器负责更改页面。

<%= link_to 'Destroy', measurment, method: :delete, 
            data: { 
              confirm: 'Are you sure?', 
              remote: true, 
              type: 'json' 
            }, class: "delete" %>

// place this code in your packs or the assets pipeline
$(document).on('ajax:success', '.delete', function(){
  $(this).closest('tr').fadeOut();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM