How to remove selected attachment when multiple attachments are uploaded using carrierwave gem in rails 6

27 views Asked by At

I am using rails 6 app and carrierwave gem for attachments. I have post model shown below.

class Post < ApplicationRecord
    mount_uploaders :attachments, AttachmentUploader
    validates :body, presence: true
    validates :attachments, presence: {message: "is Mandatory"}
end

Below is my schema

create_table "posts", force: :cascade do |t|
    t.string "title"
    t.text "body"
    t.string "image"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.json "attachments"
  end

Below is my form.

<%= form_for post do |f| %>
  <div>
    <%= f.label :title %>
    <%= f.text_field :title %>
  </div>
  <div>
    <%= f.label :body %>
    <%= f.text_area :body %>
  </div>

  <div>
    <%= f.label :attachments %>
    <%= f.file_field :attachments, multiple: true %> 

    <% post.attachments.each do |attachment| %>
    <%= image_tag(attachment.thumb.url) %>
    <%= label_tag :remove_attachments do %>
        Remove Attachment
        <%= f.check_box :remove_attachments %>
      <% end %>
    <% end %>
  </div>

  <%= f.submit %>
<% end %>
<%= render 'shared/errors', object: post %>

The same form is being used in both create and edit form.

Below is my controller.

class PostsController < ApplicationController
    before_action :set_post, only: [:show, :edit, :update]
    def index
      @posts = Post.order('created_at DESC')
    end
    def show
    end
    def new
      @post = Post.new
    end
    def create
      @post = Post.new(post_params)
      if @post.save
        redirect_to posts_path
      else
        render :new
      end
    end
    def edit
    end
    def update
      if @post.update_attributes(post_params)
        redirect_to post_path(@post)
      else
        render :edit
      end
    end
    private
    def post_params
        params.require(:post).permit(:title, :body, :image, :remove_image, :image_cache, :remove_attachments, :attachments_cache, attachments: [])
    end
    def set_post
      @post = Post.find(params[:id])
    end
end

Below is the picture where I uploaded multiple attachments and opened the record in edit mode. enter image description here

Now when I check the checkbox of first image and click update button, it does not gets deleted. following are its logs.

Started PATCH "/posts/49" for ::1 at 2024-02-07 16:52:14 +0500 Processing by PostsController#update as HTML Parameters: {"authenticity_token"=>"msl+DKCUn0pc1EV+C4bBF/vq9xhWcfBD+OWdQZA99uvHFldTTf9wCov68RU4ms0Qra+L1cA8xh6X7Xu+WyGdZg==", "post"=>{"title"=>"asdasd", "body"=>"asdasd", "remove_attachments"=>"0"}, "commit"=>"Update Post", "id"=>"49"}

Now when I click even both the first and second attachment checkboxes, logs and params remain the same and nothing gets deleted. Now When I only click the check box of last attachment, then my all attachments gets deleted. following are the logs and ss.

enter image description here

Started PATCH "/posts/49" for ::1 at 2024-02-07 16:55:34 +0500 Processing by PostsController#update as HTML Parameters: {"authenticity_token"=>"lxvxjIQFvORLBGMGtphY+tiY3l3T1MR8g1t1InyqHVPKxNjTaW5TpJwq122FhFT9jt2ikEWZ8iHsU5Pdt7Z23g==", "post"=>{"title"=>"asdasd", "body"=>"asdasd", "remove_attachments"=>"1"}, "commit"=>"Update Post", "id"=>"49"} UPDATE "posts" SET "attachments" = $1, "updated_at" = $2 WHERE "posts"."id" = $3 [["attachments", nil], ["updated_at", "2024-02-07 11:55:34.854597"], ["id", 49]]

I dont know what is the reason of this strange behaviour or this behavior is by default or I have miss-configured something. Kindly review this. I want only to delete selected files not all at once.

0

There are 0 answers