我在我的应用中使用的宝石有Prawn,Cloudinary,Carrierwave和ckEditor。
我使用Cloudinary来存储图像。在Carrierwave的帮助下,我使用ckEditor(WYSIWYG)将图像上传到Cloudinary。
下面是一个完整的ckEditor上传器。 ckeditor_picture_uploader.rb
class CkeditorPictureUploader < CarrierWave::Uploader::Base
include Ckeditor::Backend::CarrierWave
include Cloudinary::CarrierWave
include CarrierWave::MiniMagick
[:extract_content_type, :set_size, :read_dimensions].each do |method|
define_method :"#{method}_with_cloudinary" do
send(:"#{method}_without_cloudinary") if self.file.is_a?(CarrierWave::SanitizedFile)
{}
end
alias_method_chain method, :cloudinary
end
process :read_dimensions
# Create different versions of your uploaded files:
version :thumb do
process :resize_to_fill => [118, 100]
end
version :content do
process :resize_to_limit => [800, 800]
end
# Add a white list of extensions which are allowed to be uploaded.
# For images you might use something like this:
def extension_white_list
Ckeditor.image_file_types
end
end
在Publication(我使用ckEditor的地方)控制器show动作我指定了Prawn使用的参数:
respond_to do |format|
format.html
format.pdf do
pdf = PublicationPdf.new(@publication)
send_data pdf.render, filename: "#{@publication.title}.pdf",
type: "application/pdf",
disposition: "inline"
我在指定维度时使用单独的文件来生成PrawnPDF。 publication_pdf.rb:
class PublicationPdf < Prawn::Document
require "open-uri"
def initialize(publication)
super(top_margin: 50)
@publication = publication
ckeditor_pic
end
def ckeditor_pic
image open("http://res.cloudinary.com/long_path/f29d54371fa9d7_development.jpg")
end
end
在ckeditor_pic方法中,我只是直接指向Cloudinary,看看Prawn是否会生成带图像的PDF,而且确实如此。现在我想为Prawn提供通用路径来生成PDF格式,其中包含从Cloudinary获取的图片。
我的问题是给出这个通用路径。如何向Prawn显示他必须从Cloudinary获取特定出版记录的图像?