здесь сортировать руководство, как использовать pdf с креветками для печати связанных моделей
например, у меня есть родительская модель, и у нее много автомобилей (родственная модель)
class Parent < ActiveRecord::Base
has_many :cars, dependent: :destroy
accepts_nested_attributes_for :cars, allow_destroy: :true
end
внутри моего контроллера, который я определил как показано ниже
class ParentsController < ApplicationController
def show
@parent = Parent.find(params[:id])
respond_to do |format|
format.html
format.pdf do
# here you call prawn pdf class (see below)
pdf = ParentPdf.new(@parent)
send_data pdf.render, filename: 'family.pdf',
type: 'application/pdf',
disposition: 'inline'
end
end
end
end
end
end
, вы можете создать папку и файл, как показано ниже app / pdf / parent_pdf.rb, и он наследуется от класса Prawn :: Document
class ParentPdf < Prawn::Document
def initialize(parent)
# init margin and size
super(top_margin: 5, left_margin: 5, page_size: 'A4', page_layout: :landscape, print_scaling: :none)
# pass argument to variable
@parent = parent
# here is you access related models like you access from your controller
@cars = @parent.cars
# you print the model
print_header
# and print related model
print_detail
end
def print_header
bounding_box([420, 510], width: 350, height: 90) do
text "name: #{@parent.name}", size: 11
end
end
def print_detail
font 'Helvetica'
font_size 9
@cars.each do |car|
text "car: #{car.name}", size: 11
end
end
end
, и последняя - это команда для распечатать pdf по гиперссылке
<%= link_to 'print pdf', parent_path(parent, format: "pdf"), :class => 'btn btn-sm btn-secondary' %>