首页 文章

使用Prawn和Ruby on Rails生成PDF

提问于
浏览
0

我've been following Ryan Bate'(优秀一如既往)但遇到了一个我似乎无法解决的问题 .

我有我的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对象,它会返回(测试数据)

#<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: "">]>

1 回答

  • 3

    当你覆盖父的 initialize 方法时,调用没有参数的 super 隐式传递所有参数,但由于 Prawn::Document 的初始化采用了一个选项哈希(与你传递的不同),它试图从vegeteaux中提取一些键 .

    通过传入父类所期望的任何参数来调用 super ,或者添加括号以明确表示您没有传递任何内容:

    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
    

相关问题