【问题标题】:Rails + CKEditor + Paperclip + AWS S3 to upload files in different foldersRails + CKEditor + Paperclip + AWS S3 上传不同文件夹中的文件
【发布时间】:2017-07-09 06:10:29
【问题描述】:

我能够设置 CKEditor 以允许在我的 Rails 应用程序中编辑 Article 模型,它会通过 Paperclip 将任何图像上传到 AWS S3。上传成功。

但是,所有上传的图像都没有分成文件夹。当我的应用最终有很多文章时,这将导致问题。

例如,要编辑任何一篇文章中的图片,用户必须滚动查看整个图片列表,即使是那些不属于争用文章的图片,才能找到正确的图片。

有没有办法以有组织的方式上传图像,例如根据每个articletitleid 命名空间?

提前致谢!

【问题讨论】:

    标签: ruby-on-rails ckeditor paperclip


    【解决方案1】:

    你可以试试——

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

    您可以传递存储桶名称,存储桶的路径将为 my_bucket_name/development/image/1/thumbnail/my_thumbnail.jpg

    我相信你也可以在 :bucket 中调用方法或块来动态处理文件夹的名称..类似-

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

    您可以像任何其他模型对象一样访问它 -

    @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
    

    你可以试试这个。

    希望对你有帮助。

    【讨论】:

    • 嗨 Milind,我如何在 ckeditor 窗口中访问它?
    • 您可以像@picture.attachment.url 一样访问它。查看我的更新答案...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-22
    • 2012-03-10
    • 2013-09-04
    • 1970-01-01
    • 1970-01-01
    • 2021-05-11
    • 2014-07-17
    相关资源
    最近更新 更多