Ruby on rails Rails 5 Prawn pdf显示总表值

Ruby on rails Rails 5 Prawn pdf显示总表值,ruby-on-rails,prawn,Ruby On Rails,Prawn,在我看来,我有 <h4><%= number_to_currency @grand_total, precision: 0, unit: "EUR ", separator: "," %></h4> 我的控制器 class ProductSalesController < TicketlinesController def index params.permit! @q = Ticketline.joins(:product

在我看来,我有

<h4><%= number_to_currency @grand_total, precision: 0, unit: "EUR ", separator: "," %></h4>
我的控制器

class ProductSalesController < TicketlinesController


  def index


    params.permit!
    @q = Ticketline.joins(:product, :product => :location).group(:PRODUCT, :TICKET, :DISCOUNT_CONSIGNOR).select("PRODUCT, DISCOUNT_CONSIGNOR, UNITS, TICKET, SUM(ticketlines.PRICESELL*UNITS) AS AMOUNT, SUM(PRICE*UNITS) AS TOTAL, PRICE, UNITS, ticketlines.PRICESELL, SUM(UNITS) AS TOTALUNITS").ransack(params[:q])
    @product_sales = @q.result.paginate(:page => params[:page], :per_page => 30)
    @product_salesnp = @q.result
    @amount_total = @q.result.map(&:total_amount).sum
    @discount_total = @q.result.map(&:discount).sum
    @grand_total = @q.result.map(&:total).sum
    @consignor_cost_total = @q.result.map(&:consignor_cost).sum
    @cost_of_goods_sold_total = @q.result.map(&:cost_of_goods_sold).sum
    @gross_profit_total = @q.result.map(&:gross_profit).sum
    respond_to do |format|
    format.html

    format.pdf do
        pdf = SalesByProductPdf.new(@product_salesnp)
        pdf.render_file "report.pdf"
        send_data pdf.render, filename: 'report.pdf', type: 'application/pdf', disposition: 'inline'
      end

  end
  end
end
在由prawn生成的pdf上,我想显示相同的内容,因此我尝试输入相应的pdf.rb文件:

   class SalesByProductPdf < Prawn::Document
 include ActionView::Helpers::NumberHelper


  def initialize(product_sales)
    super()
    @product_sales = product_sales
    header
    text_content
    table_content
    footer
  end

def header 
#something 
end

def text_content
#something
 end

def table_content
#something 
end

def footer
text number_to_currency(grand_total, precision: 0, unit: "EUR ", separator: ",")
 end
end
这没有给我错误,但没有价值


正确的语法是什么?

您可以显式地将ActionView::Helpers::NumberHelper模块或您想要的任何模块包含在对虾文件中

尝试在pdf文件的初始化方法中传入@grand_total实例变量:


希望这能奏效。

谢谢你的回答。但我不确定你的代码放在哪里。我已经在我的pdf.rb def initializeproduct_sales super@product_sales=product_sales header text_content table_content footer end如果我把它放在def footer def initializegrand_total super text number_to_currencygrand_total,精度:0,单位:欧元,分隔符:,在pdf上仍然没有显示任何内容。您的@grand_total实例变量来自哪里?它是与@product_销售模型相关还是单独的模型?我已经更新了问题,以显示模型和控制器。我正在使用2个控制器Ticketlines和ProductSales用于1个型号Ticketline@grand_total是在控制器中定义的,它是在模型中定义的总和。所以当我说@grand_total id在模型上定义时犯了一个错误,因为它实际上是在控制器上定义的。这是有效的。这很有道理。我要花几个小时才能到那里。谢谢!
   class SalesByProductPdf < Prawn::Document
 include ActionView::Helpers::NumberHelper


  def initialize(product_sales)
    super()
    @product_sales = product_sales
    header
    text_content
    table_content
    footer
  end

def header 
#something 
end

def text_content
#something
 end

def table_content
#something 
end

def footer
text number_to_currency(grand_total, precision: 0, unit: "EUR ", separator: ",")
 end
end
class SalesByProductPdf < Prawn::Document
  include ActionView::Helpers::NumberHelper

  def initialize(product_salesnp, grand_total)
    super()
    @product_salesnp = product_salesnp
    @grand_total = grand_total
    header
    text_content
    table_content
    footer
  end

   ...

  def footer
    text number_to_currency(@grand_total, precision: 0, unit: "EUR ", separator: ",")
  end
end
format.pdf do
  pdf = SalesByProductPdf.new(@product_salesnp, @grand_total)
  pdf.render_file "report.pdf"
  send_data pdf.render, filename: 'report.pdf', type: 'application/pdf', disposition: 'inline'
end