3

我正在尝试通过 Prawn 将 pdf 生成添加到我的 Rails 3 应用程序中。我正在关注 Railscast,一切都很顺利,直到我尝试将一个实例变量从我的控制器传递给我创建的单独类。

控制器动作如下所示:

def show #shows some material
@material = Material.find(params[:id])
respond_to do |format|
  format.html
  format.pdf do
    pdf = MaterialPdf.new(@material)
    send_data pdf.render, filename: "material_#{@material.id}.pdf", 
                          type: "application/pdf",
                          disposition: "inline"
  end
end

结尾

material_pdf.rb 文件如下所示:

class MaterialPdf < Prawn::Document
  def initialize(material)
    super
    @material = material
    text "Placeholder text"
  end
end

我从日志中得到的错误信息很奇怪:

Material Load (0.1ms)  SELECT "materials".* FROM "materials" WHERE "materials"."id" = ? ORDER BY materials.created_at DESC LIMIT 1  [["id", "27"]]
DEPRECATION WARNING: You're trying to create an attribute `info'. Writing arbitrary attributes on a model is deprecated. Please just use `attr_writer` etc. (called from initialize at .../app/pdfs/material_pdf.rb:3)

这很奇怪,因为查询看起来很好,而且我没有尝试创建属性“信息”。我不明白。帮助。

4

1 回答 1

2

试图创建该属性的是对虾:

http://prawn.majesticseaacreature.com/docs/0.11.1/Prawn/Document.html

我认为更好地解释它:

创建属性“货币”的弃用警告

无论如何,我认为你实际上是在错误地调用超级。Document.new 采用一个选项散列:

def initialize(options={},&block)

所以你需要将它而不是材料实例传递给超类:

class MaterialPdf < Prawn::Document
  def initialize(material, prawn_opts = {})
    super(prawn_opts)
    @material = material
    text "Placeholder text"
  end
end
于 2013-02-27T22:05:25.333 回答