Rails file upload (paperclip) on edit

后端 未结 2 598
清酒与你
清酒与你 2021-01-01 14:11

I made myself a simple rails blogging-type app where I use Paperclip to upload image files.I have everything working fine and dandy. I even have it hooked up to an S3 bucket

2条回答
  •  一生所求
    2021-01-01 14:52

    The above method didn't work for me with multiple fields.

    I'm used javascript to do this as I am also using cocoon to dynamically add multiple images, on the edit form I am displaying all images with their own delete buttons. I then count the number of image objects and hide that same amount of image file upload boxes. I currently have it set for multiple but with just a little change it could work for your case. After delete, they are redirected back where in your case, count would be 0 and it would show the upload box.

    This is the nested form section

    Upload Images

    <% @rental.property_photos.each do |i| %>
    <%= image_tag i.avatar.url(:thumb), class:"img-responsive property-images" %> <%= link_to i, method: :delete, data: { confirm: 'Are you sure?' } do %> <% end %>
    <% end %>
    <%= f.fields_for :property_photos do |p| %> <%= render 'property_photo_fields', :f => p %> <% end %>

    My nested form

    <%= f.file_field :avatar %>

    My javascript

    $(document).ready(function () {
    
         var count = $('.pic').length;
    
         function hideElements(x) {
             $('.picUploader').slice(0,x).hide();
         }
    
         hideElements(count);
    
    })
    

    with a little css your results then look like this...

提交回复
热议问题