Chris
Chris

Reputation: 1074

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.

I have my Prawn::Document rendering using static content fine, ie with

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

pdf = PrintPdf.new(@vegetaux)

& this in the pdf object

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...

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

<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?

Thanks!

If I inspect the @vegetaux object with @vegetaux.inspect it returns (test data)

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

Upvotes: 1

Views: 1455

Answers (1)

Unixmonkey
Unixmonkey

Reputation: 18784

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.

Call super by passing in any arguments the parent class expects, or add the parenthesis to make it obvious you aren't passing anything:

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

Upvotes: 4

Related Questions