Skip to content

Instantly share code, notes, and snippets.

@HK49
Last active February 10, 2017 11:43
Show Gist options
  • Save HK49/acdf0050eb1fc2adcf10f75373e1d601 to your computer and use it in GitHub Desktop.
Save HK49/acdf0050eb1fc2adcf10f75373e1d601 to your computer and use it in GitHub Desktop.
Update action for Carrierwave 1.0 Multifile upload with single controller (delete several files or delete all)
<%= content_tag :div, :class => "gallery_rows" do -%>
<% gallery.photo.each do |image| %>
<%= content_tag :div, :class => "gallery_image", :id => "gallery_image_#{gallery.photo.index(image)+1}" do -%><%# delete by index %>
<%= content_tag :div, :class => "imagethumb" do -%>
<%= image_tag(image.thumb.url) if gallery.photo? -%>
<% end %>
<p>Delete photo?</p>
<div class="imageswitch">
<%= check_box_tag 'delete_this_images[]', gallery.photo.index(image),
false, id: "delete_image_#{gallery.photo.index(image)+1}", class: 'imageswitch-cb'
-%>
<%=
label_tag "delete_image_#{gallery.photo.index(image)+1}", nil, class: 'imageswitch_label' do
concat content_tag(:div, '', class: 'imageswitch-inner')
concat content_tag(:div, '', class: 'imageswitch-switch')
end
-%>
</div>
<% end %>
<% end %>
<% end unless gallery.new_record? || !gallery.photo? %>
module ArrayRefinement
refine Array do
def delete_at_multi(arr)
arr = arr.sort.reverse!
arr.each { |i| self.delete_at i }
self
end
end
end
=begin
from http://stackoverflow.com/a/7964648/4814907
delete several by index items from array
delete highest indexes first: iterate in reverse, so
it deletes behind the iteration not ahead of it, so
elements are not shifted
array.delete_at is mutating the array while you are iterating.
does not save db if arr is nil, see controller
=end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment