rails 3.2: confirm delete. In development confim dialogs opens once, but in production it opens three times

StackOverflow https://stackoverflow.com/questions/23291924

Вопрос

I have a rails form that includes some links to delete sub-objects via remote. These links have a confirm option. In development, this works fine. However, in production when I click on the delete link, the confirmation dialogue opens three times. That is, the confirmation dialogue appears, the user clicks on OK and the dialogue appears again. They click OK, and it appears again. Then on the third attempt it goes, and the deletion occurs correctly.

This is the form code:

<% 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 %>

Notifications is the main object, and SupportingDocument is the sub-object being deleted.

This is the JavaScript for theUnexpected.successRefreshList

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

which on success, replaces the list of supporting documents with the current list. Note that this list includes a new set of deletion links.

Can anyone point me in the right direction as to why the confirmation dialogue is opening three times in production?

Это было полезно?

Решение

I've found the problem. The 'production' environment wasn't actually 'production', it was a custom 'uat' environment. The uat configuration file had been created by copying config/environment/development.rb so that all noisy error messages were still generated in uat, so as to make bug fixing easier. However, the asset pipeline configuration had been left as used in development. The application was being deployed via a standard capistrano deployment script which was compiling the assets.

This meant that all the assets (including the JavaScript files) where being loaded both individually, and via the precompiled asset pipeline. That is, all the JavaScript code was duplicated.

I've modified the config/environment/uat.rb and the confirmation dialogue duplication has gone away:

  # 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
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top