简体   繁体   English

Rails + CKEditor + Paperclip + AWS S3可将文件上传到其他文件夹中

[英]Rails + CKEditor + Paperclip + AWS S3 to upload files in different folders

I was able to set up CKEditor to allow editing of Article model in my Rails app, and it will upload any images to AWS S3 via Paperclip. 我能够设置CKEditor以允许在我的Rails应用中编辑Article模型,并且它将通过Paperclip将任何图像上传到AWS S3。 The upload is successful. 上传成功。

However, all the images uploaded are not separated into folders. 但是,所有上传的图像都不会分成文件夹。 This will cause problems when my app eventually has many many articles. 当我的应用最终有很多文章时,这将引起问题。

For example, to edit a picture in any one of the articles, the user will have to scroll through the whole list of pictures, even those pictures that does not belong to the article of contention, just to find the correct one. 例如,要编辑任何一篇文章中的图片,用户将不得不滚动浏览整个图片列表,即使是那些不属于争用文章的图片,也只是为了找到正确的图片。

Is there a way to upload the images in an organised manner, like namespacing according to each article 's title or id ? 是否可以通过一种有组织的方式来上传图像,例如根据每篇articletitleid来命名空间?

Thanks in advance! 提前致谢!

YOu can try something like-- 您可以尝试-

has_attached_file :picture,
  :storage => :s3,
  :bucket => 'your_bucket',
  :path => "#{Rails.env}/:attachment/:id/:style/:filename.:extension"

you can pass the bucket name and the path for your bucket will be my_bucket_name/development/image/1/thumbnail/my_thumbnail.jpg 您可以传递存储桶名称,存储桶的路径将为my_bucket_name / development / image / 1 / thumbnail / my_thumbnail.jpg

i believe you can also call method or block in :bucket to dynamically handle the names of the folder..something like- 我相信您也可以在:bucket中调用method或block来动态处理文件夹的名称..类似-

...
:bucket => get_folders,
...
...

###somewhere in model.rb
def get_folders
 if self.type = 'Admin'
   "admin"
 else
   "user"
end

You can access it like any other model object - 您可以像访问任何其他模型对象一样访问它-

@picture = Picture.find(params[:id])
###url of the picture
url = @picture.my_attachment_name.url
##considering above scenario,where picture is attachment name
url = @picture.picture.url

you can give this a try. 您可以尝试一下。

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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