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

Controller Action 如下所示:

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)

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

最佳答案

试图创建该属性的是 Prawn :

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

我认为解释得更好:

Deprecation warning for creating attribute 'currency'

无论如何,我认为你实际上是错误地调用了 super 。 Document.new 采用一个选项哈希:

def initialize(options={},&block)

因此,您需要将其传递给父类(super class),而不是将 Material 实例传递给父类(super class):
class MaterialPdf < Prawn::Document
  def initialize(material, prawn_opts = {})
    super(prawn_opts)
    @material = material
    text "Placeholder text"
  end
end

关于ruby-on-rails-3 - Rails 3.2.8 中的弃用警告 : You're trying to create an attribute `info' ,,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15121478/

10-13 02:19