我正在使用Barby gem:
在回调中创建条形码.pngIsbn.rb:
before_save :barcode
def barcode
barcode = Barby::Bookland.new(self.productidentifier_idvalue)
my_bc = Barcodeimg.new(:isbn_id => self.id)
my_bc.image = File.open("#{self.productidentifier_idvalue}-barcode.png", 'w') do |f|
f.write barcode.to_png
end
my_bc.save!
end
然后我希望通过paperclip将它直接上传到S3:这是Barcodeimg.rb模型:
require 'open-uri'
require "aws/s3"
class Barcodeimg < ActiveRecord::Base
has_attached_file :image,
:storage => :s3,
:s3_credentials => "#{Rails.root}/config/s3.yml",
:s3_protocol => "https",
:path => ":class/:id/:basename_:style.:extension",
:bucket => "bucketname",
:url => ":s3_eu_url"
end
我知道凭据有效,因为我在另一个模型中使用完全相同的设置。知道我做错了什么吗?正在保存新的Barcodeimg实例,但没有附件。没有错误消息 - S3上没有出现任何图像!从堆栈跟踪:
SQL (1.8ms) INSERT INTO "barcodeimgs" ( "created_at", "image_content_type", "image_file_name", "image_file_size", "image_remote_url", "isbn_id", "updated_at", "user_id") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) ["created_at", Tue, 30 Aug 2011 20:26:20 UTC +00:00], ["image_content_type", nil], ["image_file_name", nil], ["image_file_size", nil], ["image_remote_url", nil], ["isbn_id", 7128], ["updated_at", Tue, 30 Aug 2011 20:26:20 UTC +00:00], ["user_id", nil]]
[paperclip] Saving attachments.
答案 0 :(得分:1)
File.open将返回块的结果。这意味着您要将文件的长度写入附件。
我会将块替换为:
f = File.open("#{self.productidentifier_idvalue}-barcode.png", 'w+')
f.write barcode.to_png
my_bc.image = f
另一个问题可能是当前目录的权限。你有权在那里写作吗?