title: Rails - Editing Attachments with CarrierWave & Cocoon
body: <p>I have two models, <strong>Evidence</strong> and <strong>Attachment</strong>. An evidence has many attachments, and I'm using the Cocoon gem with a nested form to create/edit Evidences (and their associated Attachments). I'm using CarrierWave to handle the attachment uploads.</p>
  
  <p>I can easily create an Evidence with multiple attachments, but once an Evidence has been created, I can't seem to add additional attachments. The form works fine but the attachments do not save.</p>
  
  <p>Here's the basic setup of my code:</p>
  
  <p>(app/models/evidence.rb)</p>
  
  <pre><code>class Evidence &lt; ActiveRecord::Base
    has_many :attachments, as: :attachable
    accepts_nested_attributes_for :attachments, allow_destroy: true
  end
  </code></pre>
  
  <p>(app/models/attachment.rb)</p>
  
  <pre><code>require 'digest'
  
  class Attachment &lt; ActiveRecord::Base
    mount_uploader :attachment, AttachmentUploader
    belongs_to :attachable, polymorphic: true
    attr_accessor :override_filename
    delegate :file, to: :attachment
  
    after_create do
      AttachmentFilesizeJob.perform_later(self)
      AttachmentMd5sumJob.perform_later(self)
    end
  end
  </code></pre>
  
  <p>(app/views/evidences/_form.html.erb)</p>
  
  <pre><code>&lt;%= simple_form_for(@evidence) do |f| %&gt;
    &lt;%= f.error_notification %&gt;
  
    &lt;div class="panel panel-default"&gt;
      &lt;div class="panel-heading"&gt;
        &lt;h3 class="panel-title"&gt;Attachments&lt;/h3&gt;
      &lt;/div&gt;
      &lt;div class="panel-body"&gt;
  
        &lt;table class="table table-condensed table-striped"&gt;
          &lt;thead&gt;
            &lt;tr&gt;
              &lt;th&gt;File&lt;/th&gt;
              &lt;th colspan="1"&gt;&lt;/th&gt;
            &lt;/tr&gt;
          &lt;/thead&gt;
          &lt;tbody id="attachments_table"&gt;
            &lt;%= f.fields_for :attachments do |af| %&gt;
              &lt;% if af.object.attachment.nil? %&gt;
                &lt;%= render 'attachment_fields', f: af %&gt;
              &lt;% else %&gt;
                &lt;tr&gt;
                  &lt;td&gt;&lt;code&gt;&lt;%= af.object.attachment %&gt;&lt;/code&gt;&lt;%= attachment_download_button(af.object) %&gt;&lt;/td&gt;
                &lt;/tr&gt;
              &lt;% end %&gt;          
            &lt;% end %&gt;
          &lt;/tbody&gt;
        &lt;/table&gt;
  
        &lt;div id="links"&gt;
          &lt;%=
            link_to_add_association 'Add Attachment', f, :attachments,
              :"data-association-insertion-node" =&gt; 'tbody#attachments_table',
              :"data-association-insertion-method" =&gt; 'append',
              class: 'btn btn-primary btn-sm'
            %&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;  
  
    &lt;div class="form-actions"&gt;
      &lt;%= f.button :submit %&gt;
    &lt;/div&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>(app/views/evidences/_attachment_fields.html.erb)</p>
  
  <pre><code>&lt;tr class="nested-fields form-inline form-table-row"&gt;
    &lt;td&gt;
      &lt;%= f.file_field :attachment %&gt;
    &lt;/td&gt;
  &lt;/tr&gt;
  </code></pre>
  
  <p>What can I do here to enable adding attachments to an already created evidence? Everything works fine when creating a new evidence, but not when editing.</p>
  
  <p>Params on Evidence#create (with two attachments - strains1.csv and strains2.csv)</p>
  
  <blockquote>
    <p>Parameters: {"utf8"=>"✓",
    "authenticity_token"=>"rKq1zaDAc1w1GlgF8Q+ejjVuA6YXYc5EfhCwHXltK/4vijOJw1ffeHUY3GWSSmKLevbfxBSwMAeEIjuHw4BIhQ==",
    "evidence"=>{"source"=>"", "notes"=>"", "mutation_id"=>"",
    "attachments_attributes"=>{"1476226326082"=>{"attachment"=>#,
    @original_filename="strains1.csv", @content_type="text/csv",
    @headers="Content-Disposition: form-data;
    name=\"evidence[attachments_attributes][1476226326082][attachment]\";
    filename=\"strains1.csv\"\r\nContent-Type: text/csv\r\n">},
    "1476226412102"=>{"attachment"=>#,
    @original_filename="strains2.csv", @content_type="text/csv",
    @headers="Content-Disposition: form-data;
    name=\"evidence[attachments_attributes][1476226412102][attachment]\";
    filename=\"strains2.csv\"\r\nContent-Type: text/csv\r\n">}}},
    "commit"=>"Create Evidence"}</p>
  </blockquote>
  
  <p>These two attachments save fine. Then I try to edit the evidence and add an additional attachment, "strains3.csv" - here are the params on Evidence#update:</p>
  
  <blockquote>
    <p>Parameters: {"utf8"=>"✓",
    "authenticity_token"=>"QsYgeVnsTf8oegVaJ733JD93MPxnH1aXutC9dDDAf53B5qY9Onvh22h4gTpE+AshcO/snmTOqNRA4jbuii0c5g==",
    "evidence"=>{"source"=>"", "notes"=>"", "mutation_id"=>"",
    "attachments_attributes"=>{"0"=>{"id"=>"87"}, "1"=>{"id"=>"88"},
    "1476226681765"=>{"attachment"=>#,
    @original_filename="strains3.csv", @content_type="text/csv",
    @headers="Content-Disposition: form-data;
    name=\"evidence[attachments_attributes][1476226681765][attachment]\";
    filename=\"strains3.csv\"\r\nContent-Type: text/csv\r\n">}}},
    "commit"=>"Update Evidence", "id"=>"1"}</p>
  </blockquote>
  
  <p>The third attachment never gets uploaded or saved.</p>