问题
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.
My uploader looks like this:
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 %>
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.
Controller:
key = params[:key].split('/').last(2).join('/')
@document = Document.new(:filename_key => key)
Model:
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 ...
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.
Any suggestions of where to look next are appreciated in advance!
回答1:
I found I had a asked a similar question almost a year ago. It turns out the solution was the same for both.
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.
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
.
来源:https://stackoverflow.com/questions/25710418/carrierwavedirect-without-processing