我花了很多时间尝试将文件附加到 mongoid 回形针字段中。我发现的例子总是做同样的事情:
my_model_instance = MyModel.new
file = File.open(file_path)
my_model_instance.attachment = file
file.close
my_model_instance.save!
正如您在如何使用 Paperclip 以编程方式设置文件上传中所见。
我做的模型是:
class Logotype
include Mongoid::Document
include Mongoid::Paperclip
has_mongoid_attached_file :logo,
styles: {large: ['640x160'], small: ['300x300>']},
size: { in: 0..3.megabytes },
content_type: [ "image/jpg", "image/png", "image/bmp" ],
storage: :filesystem,
path: ':rails_root/public/resource/resources/:id/:style.:extension',
url: '/logos/resources/:id/:style.:extension'
validates_presence_of :logo
validates_uniqueness_of :logo
end
但是我总是犯同样的大错误:
NoMethodError: undefined method `__bson_dump__' for /logos/resources/51b5bfaa69fd8a7941000005/original.jpg?1370865578:Paperclip::Attachment
...
有人可以帮助我吗?提前致谢!
PD:对不起我的英语水平。