简体   繁体   中英

Upload file to parent's folder (Carrierwave + s3)

I have a model "Image" with an uploader attached to it. Image belongs to multiple models. I'm using s3 to host my files.

Right now my path is: "image/file.jpg"

Once an image is uploaded I want the path to be "parent-model/image/file.jpg". How can I accomplish this?

Thanks.

file_uploader.rb

   def store_dir
      "#{model.class.to_s.underscore}"
   end

image.rb

class Image < ActiveRecord::Base
    belongs_to :imageable, :polymorphic => true

    mount_uploader :file, FileUploader
end

To upload file to proper dir you need to change the store_dir method to

def store_dir
  "#{model.imageable.class.to_s.underscore"/images/"
end

or maybe even

"#{model.imageable_type.underscore"/images/"

(you need to try this second one out, should work if association was persisted before.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM