[英]Rails/Prawn: how do I use rails helpers inside a Prawn class?
I'm trying to use rails 3.2
helpers inside a prawn
class, but rails throws: 我正在尝试在一个prawn
使用rails 3.2
帮助程序,但是rails会抛出:
undefined method `number_with_precision' for #<QuotePdf:0x83d4188>
Prawn Class 虾类
class QuotePdf < Prawn::Document
def initialize(quote)
super()
text "sum: #{number_with_precision(quote.sum)}"
end
end
Controller 调节器
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
Thanks for your help. 谢谢你的帮助。
You have to explicitly include ActionView::Helpers::NumberHelper
(or any other helper class/module) in your prawn document class. 您必须在您的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
Just pass the view_context
to the Prawn sub-class initializer. 只需将view_context
传递给Prawn子类初始化程序。
def initialize(quote, view_context)
super()
@view = view_context
end
in Controller, change to: 在控制器中,更改为:
QuotePdf.new(@quote, view_context)
then in Prawn sub-class, this will work: 然后在Prawn子类中,这将起作用:
@view.number_with_precision(quote.sum)
如果iafonov解决方案不起作用,您可能只需要包含不带前缀的NumberHelper
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.