rails 3.2:确认删除。在开发确认对话框中打开一次,但在生产中它会打开三次

时间:2014-04-25 11:35:36

标签: javascript jquery ruby-on-rails ajax confirm

我有一个rails表单,其中包含一些通过remote删除子对象的链接。这些链接有一个确认选项。在开发中,这很好用。但是,在点击删除链接的生产中,确认对话框打开三次。也就是说,出现确认对话框,用户点击确定并再次出现对话框。他们单击确定,然后再次出现。然后在第三次尝试时,它会正确删除。

这是表格代码:

<% delete_link_id = ['delete','notification', @notification.id, 'supporting_document', supporting_document.id, Time.now.to_i].join('_') %>
<%= link_to(
      'Delete',
      notification_supporting_document_path(@notification, supporting_document),
      method: 'delete',
      confirm: "Are you sure you want to delete #{supporting_document.name}?",
      remote: true,
      id: delete_link_id,
      class: 'btn btn-danger'
    )
%>
<%= javascript_tag do %>
  $(function() {
    theUnexpected.successRefreshList('#<%= delete_link_id %>');
  });
<% end %>

通知是主要对象,SupportingDocument是要删除的子对象。

这是theUnexpected.successRefreshList的JavaScript

theUnexpected.successRefreshList = function(anchor){
  $(anchor).on('ajax:success',function( event, data, status, xhr ) {
    $('#supporting_documents .list').html(data);
  });
};

成功时,用当前列表替换支持文档列表。请注意,此列表包含一组新的删除链接。

有人能指出正确的方向,为什么确认对话在生产中开放三次?

1 个答案:

答案 0 :(得分:0)

我发现了问题。 “生产”环境实际上不是“生产”环境,而是一种定制的“生产”环境。通过复制config / environment / development.rb创建了uat配置文件,以便仍然在uat中生成所有嘈杂的错误消息,从而使错误修复更容易。但是,资产管道配置已保留在开发中使用。该应用程序正在通过标准capistrano部署脚本进行部署,该脚本正在编译资产。

这意味着所有资产(包括JavaScript文件)都可以单独加载,也可以通过预编译的资产管道加载。也就是说,所有的JavaScript代码都是重复的。

我修改了config / environment / uat.rb,确认对话重复已经消失:

  # Code is not reloaded between requests
  config.cache_classes = true

  # Log error messages when you accidentally call methods on nil.
  config.whiny_nils = true

  # Show full error reports and enable caching
  config.consider_all_requests_local       = true
  config.action_controller.perform_caching = true

  # Disable Rails's static asset server (Apache or nginx will already do this)
  config.serve_static_assets = false

  # Compress JavaScripts and CSS
  config.assets.compress = true

  # Don't fallback to assets pipeline if a precompiled asset is missed
  config.assets.compile = false

  # Generate digests for assets URLs
  config.assets.digest = true