Nested files form with delete check-boxes

16 views
Skip to first unread message

Walter Davis

unread,
Nov 27, 2015, 3:05:12 PM11/27/15
to SimpleForm
Can anyone point out an example of a nested form with file inputs and the _delete checkboxes needed for Carrierwave?

Here's the setup:

class Candidate < ActiveRecord::Base
  has_many :documents
  accepts_nested_attributes_for :documents, reject_if: :all_blank, allow_destroy: true
end

class Document < ActiveRecord::Base
  belongs_to :candidate
  mount_uploader :file, DocumentUploader
  def label
    return style.titleize unless style == 'CV'
    'CV'
  end
  def file_name
    file.to_s.split('/').last
  end
end

class CandidatesController < ApplicationController
  def edit
    @documents = []
    Candidate::STYLES.each do |style|
      @documents += @candidate.documents.where( style: style ) || @candidate.documents.build( style: style )
    end
  end
end

Now in my view, I am trying to set up the correct check box to delete the uploaded file, but I am getting errors when I try to do this with either the simple form "input" helper or the Rails form builder "check_box" helper. Do I need to step back and use check_box_tag to do this?

Walter

Walter Davis

unread,
Nov 27, 2015, 3:06:30 PM11/27/15
to SimpleForm
And I forgot the view code:

<%= simple_form_for(@candidate) do |f| %>
<%= f.error_notification %>

<div class="form-inputs">
  <%= f.input :first_name %>
  <%= f.input :last_name %>
  <%= f.simple_fields_for :documents, @documents.to_a do |d| %>
  <div class="file-field">
    <%= d.input :file, label: d.object.label, label_html: {class: 'file'} %>
    <%= d.check_box :_delete, label: 'Remove', label_html: {class: 'delete check_box'} %>
    <%= d.hint d.object.file_name.to_s %>
    <%= d.hidden_field :style, class: 'style' %>
    <%= d.hidden_field :id, class: 'id' %>
  </div>
  <p class="add"><a class="btn">Add</a></p>
  <%- end -%>
</div>

<div class="form-actions">
  <%= f.button :submit %>
</div>
<% end %>
 

Walter

walterda...@gmail.com

unread,
Nov 28, 2015, 1:09:27 PM11/28/15
to plataformate...@googlegroups.com

> On Nov 27, 2015, at 3:06 PM, Walter Davis <walterda...@gmail.com> wrote:
>
>
>
> On Friday, November 27, 2015 at 3:05:12 PM UTC-5, Walter Davis wrote:
> Can anyone point out an example of a nested form with file inputs and the _delete checkboxes needed for Carrierwave?
>
> Here's the setup:
>
> [snip]
> <%= simple_form_for(@candidate) do |f| %>
> <%= f.error_notification %>
>
> <div class="form-inputs">
> <%= f.input :first_name %>
> <%= f.input :last_name %>
> <%= f.simple_fields_for :documents, @documents.to_a do |d| %>
> <div class="file-field">
> <%= d.input :file, label: d.object.label, label_html: {class: 'file'} %>
> <%= d.check_box :_delete, label: 'Remove', label_html: {class: 'delete check_box'} %>
> <%= d.hint d.object.file_name.to_s %>
> <%= d.hidden_field :style, class: 'style' %>
> <%= d.hidden_field :id, class: 'id' %>
> </div>
> <p class="add"><a class="btn">Add</a></p>
> <%- end -%>
> </div>
>
> <div class="form-actions">
> <%= f.button :submit %>
> </div>
> <% end %>
>

The problem was that I should have used :_destroy, not :_delete as my key. Posting this for future me and whomever else misses this bit.

Walter
Reply all
Reply to author
Forward
0 new messages