5

我正在使用 Prawn 生成 PDF 文档,但在尝试为项目生成表格时出现上述错误。关于如何去做的任何想法?

应用程序/模型/storage_request.rb

class StorageRequest < ActiveRecord::Base
  has_many :packages

  accepts_nested_attributes_for :packages, :allow_destroy => true
  attr_accessible :user_id, :state, :packages_attributes
end

应用程序/模型/package.rb

class Package < ActiveRecord::Base
  belongs_to :storage_request
  has_many :items

  accepts_nested_attributes_for :items, allow_destroy: true
  attr_accessible :user_id, :state, :items_attributes
end

应用程序/模型/item.rb

class Item < ActiveRecord::Base
  belongs_to :package

  attr_accessible :name, :user_id
end

app/pdfs/storage_request_pdf.rb

class StorageRequestPdf < Prawn::Document
  def initialize(storage_request, view)
    super(top_margin: 50)
    @storage_request = storage_request
    @view = view
    list_items
  end

  def list_items
    move_down 20
    text "Summary", size: 30, style: :bold, align: :center

    table item_rows do
      row(0).font_style = :bold
      self.row_colors = ["DDDDDD", "FFFFFF"]
      self.header = true
    end
  end

  def item_rows
    @storage_request.packages.map do |package|
      package.items.map do |item|
        ([["ID", "Item Name"]] +
        [item.id, item.name])
      end
    end
  end
end
4

1 回答 1

12

您的item_rows方法返回格式错误的数组。它将标题添加到每一行并返回一个数组,如下所示:

[ [["ID", "Item Name"], 1, "Foo"],
  [["ID", "Item Name"], 2, "Bar"],
  [["ID", "Item Name"], 3, "Baz"] ]

而 Prawn 期望这样的数组:

[ ["ID", "Item Name"],
  [1, "Foo"],
  [2, "Bar"],
  [3, "Baz"] ]

您应该始终为您的代码编写测试以尽早发现此类错误。

我会在不同的方法中定义行和标题:

def item_header
  ["ID", "Item Name"]
end

def item_rows
  @storage_request.packages.map do |package|
    package.items.map { |item| [item.id, item.name] }
  end
end

def item_table_data
  [item_header, *item_rows] 
end

并使用以下命令创建表:

table(item_table_data) do
  # ...
end

item_rows方法仍然有点难看,因为它深入到对象中。我会添加一个has_many :through 关联StorageRequest

class StorageRequest < ActiveRecord::Base
  has_many :packages
  has_many :items, :through => :packages

并重构item_rows方法:

def item_rows
  @storage_request.items { |item| [item.id, item.name] }
end
于 2013-06-10T16:11:21.820 回答