【发布时间】:2014-10-31 20:32:20
【问题描述】:
我有一个包含 PDF 文件的文档模型。因为我不需要以任何方式处理 PDF,所以我尝试使用没有处理步骤的 CarrierWaveDirect,我认为这是下载和重新上传文件。
我的上传器如下所示:
class DocumentUploader < CarrierWave::Uploader::Base
include CarrierWave::RMagick
include CarrierWave::MimeTypes
include CarrierWaveDirect::Uploader
def will_include_content_type
true
end
default_content_type 'application/pdf'
allowed_content_types = %w(application/pdf)
def store_dir
prefix = Rails.env.production? ? '' : 'tmp/'
"#{prefix}files/documents"
end
def extension_white_list
%w(pdf)
end
end
我将文档(在控制器中)定义为:
@document = Document.new.filename
@document.success_action_redirect = new_document_url(:step => 2)
我正在使用直接上传表单来上传文件本身,它工作正常。
<%= direct_upload_form_for @document do |f| %>
<%= f.file_field :filename, :required => true %>
<%= f.submit "Upload Document" %>
<% end %>
当我取回密钥后,我创建了一个名为 filename_key 的属性,并且我的模型中的回调会查找此属性以更新列。
控制器:
key = params[:key].split('/').last(2).join('/')
@document = Document.new(:filename_key => key)
型号:
after_save :check_for_file
def check_for_file
unless self.filename_key.blank?
update_columns(:filename => self.filename_key.to_s)
end
end
这实际上一切正常。问题是当我去再次保存记录时,我得到了这个错误:
ActiveRecord::StatementInvalid: Mysql2::Error: Data too long for column 'filename' at row 1: UPDATE `documents` SET `filename` = '--- &1 !ruby/object:DocumentUploader\nmodel: !ruby/object:Document\n attributes:\n id: 92\n ...
它试图将属性的全部内容设置为属性本身。我的第一个猜测是我绕过了重要的 after_create 或 after_save 回调,但除非我避免这些回调,否则我无法在不处理的情况下保存文件。
任何关于下一步看哪里的建议都提前表示感谢!
【问题讨论】:
标签: ruby-on-rails upload amazon-s3 carrierwave