title: Rails Cocoon CoffeeScript Before-Insert
body: <p>Struggling to understand CoffeeScript and calling the Before-Insert Action with Cocoon in Rails... I need to filter out the Projects relating to the Company that is selected. The supplier filter works fine as it isn't related to Cocoon, but the Project filter doesn't work. Any help please?</p>
  
  <p>My CoffeeScript</p>
  
  <pre><code>ready = -&gt;
  
    $('#quote_supplier_id').parent().hide()
    suppliers = $('#quote_supplier_id').html()
    $('#quote_company_id').change -&gt;
      company = $('#quote_company_id :selected').text()
      escaped_company = company.replace(/([ #;&amp;,.+*~\':"!^$[\]()=&gt;|\/@])/g,'\\$1')
      options = $(suppliers).filter("optgroup[label='#{escaped_company}']").html()
      if options
        $('#quote_supplier_id').html(options)
        $('#quote_supplier_id').parent().show()
      else
        $('#quote_supplier_id').empty()
    $('#quote_supplier_id').parent().hide()
  
    $('#quote_transactions')
      .on('cocoon:before-insert'   
        projects = $('.projectselect').html()
        company = $('#quote_company_id :selected').text()
        escaped_company = company.replace(/([ #;&amp;,.+*~\':"!^$[\]()=&gt;|\/@])/g, '\\$1')
        optionsprojects = $(projects).filter("optgroup[label='#{escaped_company}']").html()
        if optionsprojects
          $('.projectselect').html(optionsprojects)
        else
          $('.projectselect').empty()
      )
  
  
  $(document).ready(ready)
  $(document).on('page:load', ready)
  </code></pre>
  
  <p>My Form</p>
  
  <pre><code>  &lt;tbody id="quote_transactions"&gt;
      &lt;%= f.fields_for :quote_transactions do |quote_transaction| %&gt;
        &lt;%= render 'transaction_fields', :f =&gt; quote_transaction %&gt;
      &lt;% end %&gt;
    &lt;/tbody&gt;
  &lt;/table&gt;
  
       &lt;%= link_to_add_association '&lt;i class="glyphicon glyphicon-plus"&gt; Add Transaction&lt;/i&gt;'.html_safe, f, :quote_transactions, :"data-association-insertion-node" =&gt; "tbody#quote_transactions", :"data-association-insertion-method" =&gt; "append", :partial =&gt; "transaction_fields", :type=&gt;"button", :class=&gt;"btn btn-default" %&gt;
  </code></pre>
  
  <p>My Fields to Insert (and filter)</p>
  
  <pre><code>    &lt;tr class='nested-fields'&gt;
      &lt;td&gt;
      &lt;div class="field"&gt;
        &lt;%= f.grouped_collection_select :project_id, Company.order(:name), :projects, :name, :id, :description, {:include_blank =&gt; 'Please Select Project'}, {:class =&gt; 'form-control projectselect', :onfocus=&gt;"extractId()", :onclick=&gt;"extractId()"} %&gt;
      &lt;/div&gt;
      &lt;/td&gt;
      &lt;td&gt;
      &lt;div class="field"&gt;
        &lt;%= f.text_field :description, :class=&gt;"form-control" %&gt;
      &lt;/div&gt;
      &lt;/td&gt;
      &lt;td&gt;
      &lt;div class="field"&gt;
        &lt;%= f.text_field :quote_amount, :class=&gt;"form-control" %&gt;
      &lt;/div&gt;
      &lt;/td&gt;
      &lt;td&gt;
      &lt;%= link_to_remove_association '&lt;i class="glyphicon glyphicon-remove"&gt;&lt;/i&gt;'.html_safe, f, :class=&gt;"btn btn-danger" %&gt;
      &lt;/td&gt;
      &lt;/tr&gt;
  </code></pre>