开发者

PDF generating with prawn - How can I acces variable in Prawn.generate?

I'm trying to generate pdf using Prawn

@buyer = Buyer.last
Prawn::Document.generat开发者_Go百科e("samle.pdf") do
  text "hello #{@buyer.name} world"
end

but this obviously doesn't work (only if I use class variable @@buyer), my question is what is the proper way of passing variable to Prawn::Document.generate

(I know the solution to this is prawnto but I'm experimenting little bit ...and also it's a sinatra project)


From http://rdoc.info/github/sandal/prawn/master/Prawn/Document#generate-class_method it looks like if you pass a variable in to your block it will be then evaluated in the current context. So try:

@buyer = Buyer.last
Prawn::Document.generate("samle.pdf") do |pdf|
  pdf.text "hello #{@buyer.name} world"
end

Edit: To be more clear, this means that rather than the block being evaluated inside a new Prawn::Document object, the Prawn::Document object is instead passed into the block. The block is then evaluated within the current object so your instance variables are still in scope.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜