gpt4 book ai didi

ruby-on-rails - 如何复制 CarrierWave 文件

转载 作者:太空宇宙 更新时间:2023-11-03 16:03:28 24 4
gpt4 key购买 nike

我有一个附件模型。每次在 Version 模型中更新时,我都想保存附件的旧版本。我在这方面取得了一些成功,但突然间它停止工作了。

一切似乎都正常,但当我尝试访问某个版本时,Google 会说 x 文件无法显示,因为它包含错误。 原始文件有效。

class Attachment < ActiveRecord::Base
mount_uploader :file, AttachmentUploader
has_many :versions

after_save :version

private

def version
versions.create(name: name, file: file) if file_changed?
end
end

class Version < ActiveRecord::Base
mount_uploader :file, VersionUploader
belongs_to :attachment
end

我试着改变一些东西:

  def version
versions.create(name: name, file: file, remote_file_url: file_url) if file_changed?
end

但这造成了另一个错误:尝试下载一个不通过 HTTP 提供的文件

我不确定如何调试这个问题。上传是普通的 Vanilla 。

class AttachmentUploader < CarrierWave::Uploader::Base
include CarrierWave::MimeTypes
process :set_content_type

storage :file

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

最佳答案

附件的 file 不是文件对象;它是一个 CarrierWave uploader 。您应该分配它代表的文件,而不是分配整个上传者。使用 versions.create(name: name, file: file.file)

可能会有更好的结果

关于ruby-on-rails - 如何复制 CarrierWave 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19523069/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com