title: Cocoon (Ruby Gem) not working in production
body: <p>I am currently developing an application with Ruby on Rails and am trying to implement nested attributes with the cocoon gem: With my current configurations it works on my local machine, but not in production on my server. I have ideas why it does not work, but not how to fix it.</p>
  
  <p>First, here is my manifest that includes cocoon:</p>
  
  <pre><code>application.js    
  
  ...
  //= require cocoon
  //= require jquery
  //= require jquery_ujs
  //= require bootstrap
  //= require turbolinks
  //= require chartkick
  //= require_tree .
  </code></pre>
  
  <p>I see the problem here: Cocoon is before jquery and jquery_ujs, therefore I get  the following error in the console of the developer tools:</p>
  
  <blockquote>
    <p>Uncaught ReferenceError: jQuery is not defined at cocoon.self-6874ad698cefd21cc1119b773550f61b5c1a60396460f015222af59293affe80.js?body=1:131</p>
  </blockquote>
  
  <p>Even though I have the error, locally the nested attributes still work. In production on the other hand, the button to add the association (<em>link_to_add_association</em>) does not work anymore (it is clickable, but does nothing, not even throwing an error). Of course <strong>I also tried to rearrange the manifest and put jquery and jquery_ujs before cocoon</strong>: But then cocoon does not work properly anymore: Instead of inserting one element with <em>link_to_add_association</em>, cocoon inserts two.</p>
  
  <p>This is the head of my application.html:</p>
  
  <pre><code>application.html.erb
  
  ..
    &lt;%= csrf_meta_tags %&gt;
    &lt;%= javascript_include_tag 'https://www.gstatic.com/charts/loader.js' %&gt;
    &lt;%= javascript_include_tag 'application', 'chartkick' %&gt;
    &lt;%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track': 'reload' %&gt;
    &lt;%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %&gt;
  &lt;/head&gt;
  ..
  </code></pre>
  
  <p>Here are relevant parts of my Gemfile (ruby 2.3.1p112 is installed on the production server):</p>
  
  <pre><code>Gemfile
  
  ..
  gem 'rails', '5.0.2'
  gem 'jquery-rails', '4.2.2'
  
  ..
  # Used for the dynamic forms:
  gem 'simple_form', '3.4.0'
  gem 'cocoon', '1.2.9'
  
  ..
  </code></pre>
  
  <p>I have no idea why cocoon inserts entries twice or why the application does not work in production. Does anyone have an idea how I can fix the problem? </p>