Skip to main content

How to I use Refile as Upload in a polymorphic way? I have managed to use it that way but i am struggling on destroy image after uploading

General • Asked by Aniket Rao

Here is the Schema for Uploads

create_table "uploads", force: :cascade do |t|
    t.string   "file"
    t.string   "file_id"
    t.string   "file_filename"
    t.integer  "file_size"
    t.string   "file_content_type"
    t.integer  "uploadable_id"
    t.string   "uploadable_type"
    t.datetime "created_at",        null: false
    t.datetime "updated_at",        null: false
  end

  add_index "uploads", ["uploadable_id", "uploadable_type"], name: "index_uploads_on_uploadable_id_and_uploadable_type"

Below are the model files

# app/models/upload.rb

class Upload < ActiveRecord::Base
  belongs_to :uploadable, polymorphic: true
  attachment :file

## Not working hence commented out
  # after_destroy :remove_file

  # private

  #   def remove_file
  #     file.delete
  #   end

end
# app/models/product.rb
class Product < ActiveRecord::Base
 has_many :uploads, as: :uploadable, dependent: :destroy
 accepts_attachments_for :uploads, attachment: :file, append: true

end
# app/models/spare.rb
class Spare < ActiveRecord::Base
 has_many :uploads, as: :uploadable, dependent: :destroy
 accepts_attachments_for :uploads, attachment: :file, append: true

end
# app/models/item.rb
class Item < ActiveRecord::Base
 has_many :uploads, as: :uploadable, dependent: :destroy
 accepts_attachments_for :uploads, attachment: :file, append: true

end
# app/controllers/products_controller.rb
class ProductsController < ApplicationController

  before_action :set_product, only: [:show, :edit, :update, :destroy]
  before_filter :permission_check, only: [:show, :edit, :update, :destroy]
.
.
.
.
.

private
.
.
  def product_params
    params.require(:product).permit( :product_name, ...., uploads_files: [] )
  end

end

Spares and Items also have exact same

params.require(:product).permit(uploads_files: [])

Followed as per gem documentation and got uploads working. But for weeks I have been struggling on getting the delete function working. hoping if you can give it shot and share your insight.

Thanks mate!

Cheers


For delete, I would send the Upload ID to the uploads controller and then just delete that directly. It's simplest that way because you aren't accessing anything through the polymorphic relationship. If you did, you'd have to make a ProductsUploadsController, an ItemsUploadsController, and so on. It's far more work than is necessary that way. So the easiest solution is just to have an uploads controller that you link to and add the destroy action there.


Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 27,623+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.