Skip to content

How to: keep (not replace) files on nested edits

mpoli edited this page Sep 22, 2013 · 1 revision

Keeping (not replacing) files on nested edits

Using: Rails4, Carrierwave, nested_form, simple_form and haml

Normally, when you have a Model that has many Files and you have the Files nested in the Model edit form, the File objects that already exist get replaced if you upload a new file.

To prevent that, in your view:

= simple_nested_form_for @model, :html => { :multipart => true } do |f|
  = f.simple_fields_for :files do |p|
    - if p.object.new_record?
      = p.file_field :file
    - else
      = image_tag p.object.file.url(:thumb).to_s
    %br
    = p.link_to_remove t(:remove)
  = f.link_to_add t(:add), :files

  .form-actions
    = f.button :submit, :class => 'btn-primary'
    = link_to t('.cancel', :default => t("helpers.links.cancel")), properties_path, :class => 'btn'
Clone this wiki locally