I'm trying to generate a pdf and for some reason i keep getting a no method error when i try to render the pdf. Literally no idea what to do as all i'm trying at this point is to render a blank pdf.
我正在尝试生成一个pdf,由于某些原因,当我试图呈现pdf时,我总是得到一个no方法错误。毫不夸张地说,我不知道该怎么做,因为我现在所做的就是呈现一个空白的pdf。
NoMethodError: private method `p' called for #<OrderPdfCreator:0x007f9365053a20>
OrderController
OrderController
require 'order_pdf_creator'
def print_store_invoice
# print the store invoice...
@order = Order.find_by_id(params[:id])
# create the pdf
pdf = OrderPdfCreator.new(@order)
render :attachment => pdf.p, :filename => "#{@order.id}.pdf", :layout => false
end
OrderPdfCreator.rb
OrderPdfCreator.rb
# encoding: UTF-8
require 'open-uri'
class OrderPdfCreator < BasePdfCreator
def initialize(order)
@pdf = Prawn::Document.new(:page_size => 'A4')
@pdf.font_size = 14
@order_pdf = order
file_path = File.join(Rails.root,'tmp',"#{@order_pdf.id}.pdf")
p = File.open(file_path, 'wb') { |f| f.puts @pdf.render }
end
end
BasePdfCreator.rb
BasePdfCreator.rb
# encoding: UTF-8
class BasePdfCreator
private
def blank_line
@pdf.text ' '
end
end
1 个解决方案
#1
0
The error says it all. You are calling a method you shouldn't. Specifically in this line:
错误说明了一切。你在调用一个你不应该调用的方法。特别是在这条线:
render :attachment => pdf.p, :filename => "#{@order.id}.pdf", :layout => false
Change it to:
把它改成:
render :attachment => pdf, :filename => "#{@order.id}.pdf", :layout => false
And in your initialize method, change:
在初始化方法中,改变:
p = File.open(file_path, 'wb') { |f| f.puts @pdf.render }
to:
:
@pdf
Rails (well, ruby) always returns the result of the last statement in a method (at least by default).
Rails(嗯,ruby)总是返回方法中最后一条语句的结果(至少默认情况下是这样)。
EDIT
编辑
Ok, so I see what's happening. I personally would rename the initialize
method to something else (say createPDF
) so it returns the prawn document object and not a OrderPdfCreator
object. So in your controller you would have:
好的,我看到发生了什么。我个人会将initialize方法重命名为其他东西(比如createPDF),这样它就会返回对虾文档对象,而不是OrderPdfCreator对象。在你的控制器中你会有
pdf = OrderPdfCreator.new
pdf.createPDF
send_data pdf.render, :type => 'application/pdf', disposition: 'inline'
#1
0
The error says it all. You are calling a method you shouldn't. Specifically in this line:
错误说明了一切。你在调用一个你不应该调用的方法。特别是在这条线:
render :attachment => pdf.p, :filename => "#{@order.id}.pdf", :layout => false
Change it to:
把它改成:
render :attachment => pdf, :filename => "#{@order.id}.pdf", :layout => false
And in your initialize method, change:
在初始化方法中,改变:
p = File.open(file_path, 'wb') { |f| f.puts @pdf.render }
to:
:
@pdf
Rails (well, ruby) always returns the result of the last statement in a method (at least by default).
Rails(嗯,ruby)总是返回方法中最后一条语句的结果(至少默认情况下是这样)。
EDIT
编辑
Ok, so I see what's happening. I personally would rename the initialize
method to something else (say createPDF
) so it returns the prawn document object and not a OrderPdfCreator
object. So in your controller you would have:
好的,我看到发生了什么。我个人会将initialize方法重命名为其他东西(比如createPDF),这样它就会返回对虾文档对象,而不是OrderPdfCreator对象。在你的控制器中你会有
pdf = OrderPdfCreator.new
pdf.createPDF
send_data pdf.render, :type => 'application/pdf', disposition: 'inline'