2013-10-22 5 views
2

첨부 파일 모델이 있습니다. 버전 모델에서 업데이트 될 때마다 이전 버전의 첨부 파일을 저장하려고합니다. 나는 이것으로 약간의 성공을 거두었지만 갑자기 멈추게되었다.CarrierWave 파일을 복제하는 방법

모든 것이 작동하는 것으로 보이지만 버전에 액세스하려고 시도하면 Google이 the x file cannot be displayed because it contains errors. 원본 파일이 작동한다고 말합니다.

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 

을하지만 그것은 또 다른 오류가 생성 : trying to download a file which is not served over HTTP

나는이 문제를 디버깅하는 방법을 잘 모르겠어요 있습니다. 업로드는 단순한 바닐라입니다.

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 

답변

2

첨부 파일 file은 파일 개체가 아닙니다. 그것은 CarrierWave 업 로더입니다. 전체 업 로더를 지정하는 대신, 그것이 나타내는 파일을 할당해야합니다. versions.create(name: name, file: file.file)

으로 더 좋은 결과를 얻을 수 있습니다.