我遇到的问题仅影响该应用程序在其生产环境中的情况。我们有一个 Controller 操作,用于通过创建新对象、将属性设置为彼此相等,然后将图像添加到对象来“克隆”文章对象。
这是执行此操作的 Controller 部分:
def clone_article
ba = BlogArticle.find(params[:id])
new_ba = BlogArticle.new(ba.attributes)
ba.blog_article_images.each do |blog_img|
new_ba.blog_article_images.build(:image => blog_img.image.to_file, :embedded => blog_img.embedded?)
end
new_ba.status = 'draft'
new_ba.title = "Copy of #{ba.title}"
if new_ba.save
flash[:notice] = "Clone successful"
else
if new_ba.errors.empty?
flash[:notice] = "Unknown error occurred while cloning the post"
else
error = 'Problem while cloning the post: <br>'
new_ba.errors.each {|field, msg| error += field + " " + msg + "<br>"}
flash[:error] = error
end
end
redirect_to admin_blog_articles_url
结束
问题是该脚本在引用服务器上的本地文件时可以完美运行。但在 S3 上有图像的生产环境中,我们无法从原始帖子图像中复制任何图像。我认为这可能是时间问题,就像 Controller 在完成过程之前不等待回形针将文件加载到应用程序目录中一样,但我似乎无法让任何东西工作。另外,我不是 Rails 专家,所以我有点迷失了。
谢谢
最佳答案
我也遇到了文件名被 Tempfile
破坏的问题,并最终得到了这个有点黑客的解决方案:
ba.blog_article_images.each do |blog_img|
new_img = blog_img.image.to_file
new_img.instance_variable_set("@original_filename", blog_img.image.original_filename)
def new_img.original_filename
@original_filename
end
new_ba.blog_article_images.build(:image => new_img, :embedded => blog_img.embedded?)
end
不过它确实有效。 original_filename
是 Paperclip 添加到 File
的一个方法,我们只是在这里重写它。
我还没有使用本地存储对此进行测试,但通过阅读 Paperclip 源代码,它应该可以工作。
关于ruby-on-rails - 无法使用 Paperclip gem 复制图像(使用 S3 图像的 image.to_file),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4258765/