CarrierWaveDirect没有处理CarrierWaveDirect

2023-09-11 10:06:52 作者:花淡纱窗残月明

我有一个包含PDF文件的文档模型。因为我并不需要操纵PDF文件以任何方式,我尝试使用CarrierWaveDirect没有它的处理步骤,我相信这是下载和重新上传文件。

I have a document model that consists of PDF files. Because I don't need to manipulate the PDFs in any way, I'm attempting to use CarrierWaveDirect without its processing step, which I believe is downloading and re-uploading the files.

我的上传者是这样的:

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

我定义文档(控制器)为:

I define the document (in the controller) as:

@document = Document.new.filename
@document.success_action_redirect = new_document_url(:step => 2)

我使用的是直接上传表单上传的文件本身,这是工作的罚款。

I'm using the direct upload form to upload the file itself, which is working fine.

<%= direct_upload_form_for @document do |f| %>
  <%= f.file_field :filename, :required => true %>
  <%= f.submit "Upload Document" %>
<% end %>

当我拿到钥匙后,我创建 filename_key 称为属性,并在我的模型回调查找此属性来更新列。

When I get the key back, I create an attribute called filename_key, and a callback in my model looks for this attribute to update the column.

控制器:

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

这其实一切工作正常。问题是,当我去的保存记录再次的,我得到这个错误:

This actually all works fine. The problem is when I go to save the record again, I get this error:

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的回调,但我不能得到保存文件不处理,除非我避免这些回调。

It's trying to set the entire contents of the attribute as the attribute itself. My first guess is that I am bypassing vital after_create or after_save callbacks, but I can't get to saving a file without processing unless I avoid these callbacks.

在哪里看接下来的任何建议都AP preciated提前!

Any suggestions of where to look next are appreciated in advance!

推荐答案

我发现我有问过类似的问题差不多一年前。原来的解决方案是相同的两个。

I found I had a asked a similar question almost a year ago. It turns out the solution was the same for both.

CarrierWaveDirect使用文件名作为一种方法(你猜对了!)与上传文件的文件名的工作。对我来说,更改列的名称文件解决我的问题。

CarrierWaveDirect uses filename as a method to (you guessed it!) work with the filename of the uploaded file. For me, changing the name of the column to document solved my issue.

我还应该注意到,我发现忽略了处理步骤做工精细。但是,你就失去了任何会在该步骤中被调用,例如,设置内容类型。正如我在这个问题所展示的,我设置自动内容类型,这使我仍然认为在浏览器中的PDF文件,因为它们被设置为应用程序/ PDF 二进制/八位字节流

I should also note that I've found ignoring the processing step to work fine. However, you do lose anything that would be called during that step, for example, setting the content type. As I've shown in the question, I'm setting the content type automatically, which enables me to still view the PDF files in the browser, since they are set as application/pdf vs. binary/octet-stream.

相关推荐