Home Rails 5: carrierwave gem delete single attachment
Reply: 0

Rails 5: carrierwave gem delete single attachment

Theopap
1#
Theopap Published in 2017-12-06 15:08:47Z

I'm using the the carrierwave gem with my rails app in order to upload multiple images(attachments).

The attachments are being saved inside the item model:

create_table "items", force: :cascade do |t|
  t.string "title"
  t.string "description"
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
  t.json "attachments"
end

and this the item create action along with the params permitted:

def items_create
  @categories = Category.all
  @item = Item.new(item_params)
  @item.store_id = current_store.id
  @item.account_id = current_store.account.id

  respond_to do |format|
    if @item.save
      format.html { redirect_to store_items_show_path_path(current_store), notice: 'Item was successfully created.' }
      format.json { render :template => "stores/items/show", status: :created, location: @item }
    else
      format.html { render :template => "stores/items/new" }
      format.json { render json: @item.errors, status: :unprocessable_entity }
    end
  end
end

def item_params
    params.require(:item).permit(:title, :description, attachments: [])
  end

Now I'm trying to implement How to: Add more files and remove single file when using default multiple file uploads feature from here.

and this is the setup I have:

  def create_image
    add_more_images(images_params[:attachments])
    flash[:error] = "Failed uploading attachments" unless @item.save
    redirect_back(fallback_location: root_path)
  end

  def destroy_image
    remove_image_at_index(params[:id].to_i)
    flash[:error] = "Failed deleting attachment" unless @item.save
    redirect_back(fallback_location: root_path)
  end

 private
  def add_more_images(new_images)
    attachments = @item.attachments
    attachments += new_images
    @item.attachments = attachments
  end

  def remove_image_at_index(index)
    remain_attachments = @item.attachments # copy the array
    deleted_attachment = remain_attachments.delete_at(index) # delete the target attachment
    deleted_attachment.try(:remove!) # delete attachment from S3
    @item.attachments = remain_attachments # re-assign back
    @item.remove_attachments! if remain_attachments.empty?
  end

  def images_params
    params.require(:item).permit({attachments: []}) # allow nested params as array
  end

routes.rb

match 'stores/items/:item_id/attachments/:id'=> 'stores#destroy_image', :via => :delete, :as => :remove_attachment
post "store/items/:item_id/attachments/:id"=> "stores#create_image", :as => :create_image

I got it to upload a single file with this:

<%= form_for @item, url: create_image_path(@item), method: :post do |form| %>
     <div class="form-group">
       <label class="btn btn-default">Keep old and add more images<span style="display:none;">
          <%= form.file_field :attachments, multiple: true, id: "files" %></span></label>
     </div>

         <%= form.submit "Upload new images",  :class=>"btn btn-default" %>&nbsp;&nbsp;
  <% end %>

but can't remove each attachment I loop thru with this:

<h3>Currently uploaded images</h3>
  <% @item.attachments.each_with_index do |attachments, index| #grab the index %>
    <%= image_tag(attachments.url(:mini)) %>
    <%= link_to 'Remove', remove_attachment_path(@item, index), data: { confirm: "Are you sure you want to delete this image?" }, :method => :delete %>
   <% end %>

This is the error I'm getting each time i click on remove:

ActiveRecord::RecordNotFound in StoresController#destroy_image
Couldn't find Item with 'id'=2

in this url : https://localhost:3000/stores/items/29/attachments/2

and at this line:

def set_item
  @item = Item.find(params[:id])
end

Any ideas on how to fix the remove a single attachment feature??

Thanks in advance!

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.299793 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO