我试图在rails 3.2类中使用prawn帮助程序,但是rails抛出:
undefined method `number_with_precision' for #<QuotePdf:0x83d4188>对虾类
class QuotePdf < Prawn::Document
def initialize(quote)
super()
text "sum: #{number_with_precision(quote.sum)}"
end
end控制器
def show
@quote = current_user.company.quotes.where(:id => params[:id]).first
head :unauthorized and return unless @quote
respond_with @quote, :layout => !params[:_pjax] do |format|
format.pdf do
send_data QuotePdf.new(@quote).render, filename: "Devis-#{@quote.date_emission.strftime("%d/%m/%Y")}.pdf",
type: "application/pdf"
end
end
end谢谢你的帮助。
发布于 2012-03-14 20:07:02
您必须在prawn文档类中显式地包含ActionView::Helpers::NumberHelper (或任何其他帮助类/模块)。
class QuotePdf < Prawn::Document
include ActionView::Helpers::NumberHelper # <-
def initialize(quote)
super()
text "sum: #{number_with_precision(quote.sum)}"
end
end发布于 2012-09-12 06:06:32
只需将view_context传递给Prawn子类初始化器即可。
def initialize(quote, view_context)
super()
@view = view_context
end在主计长中,改为:
QuotePdf.new(@quote, view_context)然后,在对虾子类中,这将起作用:
@view.number_with_precision(quote.sum)发布于 2012-07-11 13:25:59
如果iafonov解决方案不起作用,您可能只需包含没有前缀的NumberHelper。
https://stackoverflow.com/questions/9708884
复制相似问题