简体   繁体   English

使用Prawn和Ruby On Rails生成PDF

[英]Generating a PDF with Prawn and Ruby On Rails

I've been following Ryan Bate's Railscast tutorial (excellent as always) but have run into an issue that I cannot seem to resolve. 我一直在关注Ryan Bate的Railscast教程 (一如既往地出色),但是遇到了一个我似乎无法解决的问题。

I have my Prawn::Document rendering using static content fine, ie with 我有我的Prawn :: Document渲染,使用静态内容很好,即

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

and in the controller 并在控制器中

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

But when I try to pass in my Rails model by adding this 但是当我尝试通过添加这个来传递我的Rails模型时

pdf = PrintPdf.new(@vegetaux)

& this in the pdf object &在pdf对象中

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

I now get this error message 我现在收到此错误消息

no implicit conversion of Symbol into Integer relating to this line... no implicit conversion of Symbol into Integer与此行相关的将no implicit conversion of Symbol into Integer ...

pdf = PrintPdf.new(@vegetaux)

The object @vegetaux seems to be OK though, because in the html reponse I can loop through the individual items and display their contents, ie /print (html) this works fine @vegetaux对象似乎还可以,因为在html响应中,我可以循环浏览各个项目并显示其内容,即/ print(html)可以正常工作

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

Can anyone help explain why i'm getting this error when I try to create the PDF document? 谁能帮我解释为什么在尝试创建PDF文档时出现此错误?

Thanks! 谢谢!

If I inspect the @vegetaux object with @vegetaux.inspect it returns (test data) 如果我使用@ 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: "">]>

When you override your parent's initialize method, calling super with no arguments implicitly passes all arguments, but since Prawn::Document 's initialize takes an options hash (which is different from what you passed), it is trying to extract some keys from vegeteaux. 当您重写父级的initialize方法时,不带任何参数的super隐式地传递所有参数,但是由于Prawn::Document的initialize接受一个选项哈希(与您传递的哈希不同),因此它试图从vegeteaux中提取一些键。

Call super by passing in any arguments the parent class expects, or add the parenthesis to make it obvious you aren't passing anything: 通过传入父类期望的任何参数来调用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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM