I have a method which is generating QR code for some string using rqrcode-rails3
gem:
def generate_qrcode(string, options={})
format = options[:format] || :png
size = options[:size] || RQRCode.minimum_qr_size_from_string(string)
level = options[:level] || :h
qrcode = RQRCode::QRCode.new(string, size: size, level: level)
svg = RQRCode::Renderers::SVG::render(qrcode, options)
image = MiniMagick::Image.read(svg) { |i| i.format "svg" }
image.format "png" if format == :png
image
end
That newly generated image would be attached to the model object and stored in the database using paperclip
:
has_attached_file :qrcode_png
has_attached_file :qrcode_svg
When I have been tried to attach the image:
label.qrcode_png = generate_qrcode(label.id.to_s)
label.save!
and got an exception Paperclip::AdapterRegistry::NoHandlerError
No handler found for MiniMagick::Image:0x007fa629e71388 @path="/var/folders/fs/yf7s27kj27n3lmdywp4tgcm80000gn/T/mini_magick20120816-2170-1w6vbz.png", @tempfile=#
What's a problem with my code?
Ok, I got it. I have to return a file object instead of MiniMagick::Image
:
File.open(image.path)
Then paperclip
can find and read a file from the file system and save it.