使用Prawn和Ruby on Rails生成PDF

时间:2015-07-14 14:41:10

标签: ruby-on-rails ruby pdf prawn

我一直关注着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对象,则返回(测试数据)

#<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 个答案:

答案 0 :(得分: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