アプリで使用する宝石は、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
パブリケーション(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 を生成するには、別のファイルを使用します。出版物_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 が Cloudinary から取得した写真を含む PDF を生成するための一般的なパスを提供したいと思います。
私にとっての問題は、この一般的なパスを与えることです。特定の出版物レコードのために Cloudinary から画像を取得する必要があることを Prawn に示すにはどうすればよいですか?