我正在尝试在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
感谢您的帮助。
答案 0 :(得分:12)
您必须在您的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
答案 1 :(得分:7)
只需将view_context
传递给Prawn子类初始值设定项。
def initialize(quote, view_context)
super()
@view = view_context
end
在Controller中,切换到:
QuotePdf.new(@quote, view_context)
然后在Prawn子类中,这将起作用:
@view.number_with_precision(quote.sum)
答案 2 :(得分:5)
如果iafonov解决方案不起作用,您可能只需要包含不带前缀的NumberHelper
。