3

我有一个非常简单的 sinatra 应用程序,它使用 datamapper 和carrierwave。

我已将上传器减少到最低限度:

class PhotoUploader < CarrierWave::Uploader::Base
  include CarrierWave::MiniMagick

  storage :file
end

并有一个使用它的简单模型:

class Photo
  include DataMapper::Resource

  property :id, Serial
  property :caption, String
  property :position, Integer
  property :created_at, DateTime
  property :updated_at, DateTime

  mount_uploader :source, PhotoUploader

  belongs_to :album
end

碰巧当我使用它时:

post '/admin/album/:id/photo/create' do
  album = Album.get(params[:id])
  photo = Photo.create(params[:photo])
  album.photos << photo
  album.save!

  redirect '/admin/album'
end

photo正在数据库上正常创建,并且文件正在正常上传到缓存目录,但未移动到最终存储目标。

我已经进入了载波代码,并且可以验证cache!正在调用该方法而不是该store!方法。

我尝试使用与对象没有任何关联的上传器:

get '/lorem' do
  f = File.open('test.png')
  u = PhotoUploader.new
  u.store!(f)

  "ok"
end

在这种情况下,一切都按预期工作。



==================== 更新====================

我相信我已经找到了问题的根源。在运行调试器时,我得到了这个:

gems/dm-core-1.2.0/lib/dm-core/query.rb:1014
conditions.each do |condition|
(rdb:1) 
gems/carrierwave-0.6.2/lib/carrierwave/sanitized_file.rb:290
def sanitize(name)
(rdb:4) 
gems/carrierwave-0.6.2/lib/carrierwave/sanitized_file.rb:299
def split_extension(filename)
(rdb:4) 
gems/carrierwave-0.6.2/lib/carrierwave/sanitized_file.rb:29
self.file = file
(rdb:4) 
gems/carrierwave-0.6.2/lib/carrierwave/sanitized_file.rb:269
if file.is_a?(Hash)
(rdb:4) 
gems/carrierwave-0.6.2/lib/carrierwave/sanitized_file.rb:274
@file = file

(rdb:4) pp file
"/Users/jvalente/Projects/dummy/public/uploads/{:filename=>\"Screen Shot 2013-01-24 at 4.57.15 PM.png\", :type=>\"image/png\", :name=>\"photo[source]\", :tempfile=>#<File:/var/folders/jp/wm1gxk8558d36rh9vzkmx4jw0000gn/T/RackMultipart20130128-27245-105ybbf>, :head=>\"Content-Disposition: form-data; name=\\\"photo[source]\\\"; filename=\\\"Screen Shot 2013-01-24 at 4.57.15 PM.png\\\"\\r\\nContent-Type: image/png\\r\\n\"}"

这显然是错误的,因为它将不符合file.is_a?(Hash)条件并进入:

@file = file
@original_filename = nil
@content_type = nil

不过,我不明白为什么会发生这种情况。

4

0 回答 0