0
votes

I have CarrierWave (0.6.1) and Nested Form gem installed. I have a Resource model with many Attachments which have a FileUploader.

I have a nested form where users can upload multiple files with one resource. I am following the section on github (https://github.com/jnicklas/carrierwave) that says how to make uploads work across redisplays unfortunately it's only for 1:1 ratio.

Here's the code that I have:

<%= nested_form_for @resource, :html=>{:multipart => true } do |f| %>
    <p>
        <%= f.label :title %>
        <%= f.text_field :title %>
    </p>
    <%= f.fields_for :attachments, @attachment do |attachment_form|  %>
      <p>
          <%= attachment_form.label :file %>
          <%= attachment_form.file_field :file %>
          <%= attachment_form.hidden_field :file_cache %>
          <%= image_tag(attachment_form.file_url) if attachment_form.file? # DOESN'T WORK!!! %>
      </p>
      <%= attachment_form.link_to_remove "Remove this attachment" %>
    <% end %>
    <%= f.link_to_add "Add attachment", :attachments %>
    <p><%= f.submit %></p>
<% end %>

Everything works and it populates the file_cache variable just fine for the attachment_form however I somehow need to add the following line in there to show the user the image of the document:

<%= image_tag(attachment_form.file_url) if attachment_form.file? %>

However there's a number of problems with this. First of all attachment_form is referencing the form_builder whereas I want the actual attachment. Second, attachment knows nothing about file.

Probably need to use another type of looping mechanism, but I'm new to Ruby so any help is appreciated.

Thanks all!

1

1 Answers

1
votes

If you try this:

<%= image_tag(attachment_form.object.file_url) if attachment_form.object.file? %>

You will be able to show previous uploaded images. But if you want to display uploaded right now, you need to use something else. For example: https://github.com/blueimp/jQuery-File-Upload/wiki/jQuery-File-Upload-v4-for-Rails-3 Sorry, if I misunderstood your question.