Ask A Question

Notifications

You’re not receiving notifications from this thread.

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

Aniket Rao asked in General

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

Reply

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.

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 85,376+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.