使用 Prawn 和 Ruby 在 Rails 上生成 PDF

Generating a PDF with Prawn and Ruby On Rails

我一直在关注 Ryan Bate 的 Railscast tutorial(一如既往的出色),但 运行 遇到了一个我似乎无法解决的问题。

我的 Prawn::Document 渲染使用静态内容很好,即

class PrintPdf < Prawn::Document
  def initialize
    super
    text "Text"
  end
end

并在控制器中

def print
  @vegetaux = Vegetable.all

  respond_to do |format|
    format.html
    format.pdf do
      pdf = PrintPdf.new
      send_data pdf.render, filename: "vegetaux.pdf", type: "application/pdf", disposition: "inline"
    end
  end
end

但是当我尝试通过添加此

来传递我的 Rails 模型时
pdf = PrintPdf.new(@vegetaux)

& pdf 对象中的这个

class PrintPdf < Prawn::Document
  def initialize(vegetaux)
    super
    @vegetaux = vegetaux
    text "Text"
  end
end

我现在收到这条错误消息

no implicit conversion of Symbol into Integer 与此行有关...

pdf = PrintPdf.new(@vegetaux)

@vegetaux 对象似乎没问题,因为在 html 响应中我可以遍历各个项目并显示它们的内容,即 /print (html) 这很好用

<ul>
<% @vegetaux.each do |vegetable| %>
  <li><%= vegetable.nom_commun %></li>
<% end %>
</ul>

任何人都可以帮助解释为什么我在尝试创建 PDF 文档时出现此错误吗?

谢谢!

如果我用@vegetaux.inspect检查@vegetaux对象,它returns(测试数据)

#<ActiveRecord::Relation [#<Vegetable id: 6, nom_commun: "Basic Flower", famille_id: 1, classe: "Something Else", genre: "Genre", espece: "Espece", origine_geographique: "Earth", cycle_biologique: "normal", racine: "Something else", tige: "another thing", feuillage: "whatevs", fleur: "big skdfhkjs dhfksdhfkj hsdkjfh ksjd hfkjsdh fkjhs...", fruit: "none", graine: "something siomething", modes_de_multiplication_possibles: "lots of things", systemes_de_production_adaptes: "all kinds of things", mise_en_place_de_la_culture: "don't understand the question", calendrier_cultural: "may - july", entretien_de_la_culture: "nope", exigences_edaphiques_ideales: "whatevs", irrigation: "keep it wet", fertilisation: "keep it fertilised", problemes_phytosanitaires_et_protections_adaptees: "none", importance_economique: "very", utilisation: "eat it", diversification: "whatevs", created_at: "2014-11-10 11:37:17", updated_at: "2014-11-19 15:28:08", photo_file_name: "flower.jpg", photo_content_type: "image/jpeg", photo_file_size: 1083468, photo_updated_at: "2014-11-10 11:37:16", exigences_climatiques: "warm & sunny">, #<Vegetable id: 13, nom_commun: "qsd", famille_id: 1, classe: "dsf", genre: "sdf", espece: "sdf", origine_geographique: "", cycle_biologique: "", racine: "", tige: "", feuillage: "", fleur: "", fruit: "", graine: "", modes_de_multiplication_possibles: "", systemes_de_production_adaptes: "", mise_en_place_de_la_culture: "", calendrier_cultural: "", entretien_de_la_culture: "", exigences_edaphiques_ideales: "", irrigation: "", fertilisation: "", problemes_phytosanitaires_et_protections_adaptees: "", importance_economique: "", utilisation: "", diversification: "", created_at: "2014-11-19 14:34:18", updated_at: "2014-11-19 14:34:18", photo_file_name: nil, photo_content_type: nil, photo_file_size: nil, photo_updated_at: nil, exigences_climatiques: "">, #<Vegetable id: 9, nom_commun: "wxc", famille_id: 1, classe: "wxc", genre: "wxc", espece: "wxc", origine_geographique: "", cycle_biologique: "", racine: "", tige: "", feuillage: "", fleur: "", fruit: "", graine: "", modes_de_multiplication_possibles: "", systemes_de_production_adaptes: "", mise_en_place_de_la_culture: "", calendrier_cultural: "", entretien_de_la_culture: "", exigences_edaphiques_ideales: "", irrigation: "", fertilisation: "", problemes_phytosanitaires_et_protections_adaptees: "", importance_economique: "", utilisation: "", diversification: "", created_at: "2014-11-19 14:19:03", updated_at: "2014-11-19 14:19:03", photo_file_name: nil, photo_content_type: nil, photo_file_size: nil, photo_updated_at: nil, exigences_climatiques: "">, #<Vegetable id: 14, nom_commun: "rty", famille_id: 2, classe: "sd", genre: "qsd", espece: "qsdqs", origine_geographique: "", cycle_biologique: "", racine: "", tige: "", feuillage: "", fleur: "", fruit: "", graine: "", modes_de_multiplication_possibles: "", systemes_de_production_adaptes: "", mise_en_place_de_la_culture: "", calendrier_cultural: "", entretien_de_la_culture: "", exigences_edaphiques_ideales: "", irrigation: "", fertilisation: "", problemes_phytosanitaires_et_protections_adaptees: "", importance_economique: "", utilisation: "", diversification: "", created_at: "2014-11-19 17:59:10", updated_at: "2015-04-11 08:50:24", photo_file_name: nil, photo_content_type: nil, photo_file_size: nil, photo_updated_at: nil, exigences_climatiques: "">]>

当您重写 parent 的 initialize 方法时,调用不带参数的 super 会隐式传递 所有 个参数,但是由于 Prawn::Document 的初始化采用选项散列(与您传递的不同),它试图从 vegeteaux 中提取一些键。

通过传入 parent class 期望的任何参数来调用 super,或者添加 parenthesis 以表明您没有传递任何内容:

class PrintPdf < Prawn::Document
  def initialize(vegetaux)
    super() # I added parentheses here to call Prawn::Document.new() with no args
    @vegetaux = vegetaux
    text "Text"
  end
end