弃用警告:您正在尝试在Rails 3.2.8中创建属性“info”

时间:2013-02-27 20:14:36

标签: ruby-on-rails-3 prawn

我正在尝试通过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)

这很奇怪,因为查询看起来很好,我不是要创建一个属性'info'。我不明白。帮助

1 个答案:

答案 0 :(得分:2)

这是大虾试图创建该属性:

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

我认为更好地解释:

Deprecation warning for creating attribute 'currency'

无论如何,我认为你实际上是在调用超级错误。 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