2016-10-27 67 views
2

在我看來,我有Rails的5蝦pdf顯示總表值

<h4><%= number_to_currency @grand_total, precision: 0, unit: "EUR ", separator: "," %></h4> 

這顯示了一個列一個正確的總計。 @grand_total在我的控制器中定義,它是在模型中定義的total的總和。

我的模型

class Ticketline < ActiveRecord::Base 
    belongs_to :ticket, :foreign_key => 'TICKET' 
    belongs_to :product, :foreign_key => 'PRODUCT' 

def discount 
    (self.AMOUNT - self.TOTAL) 
end 

def total_amount 
(pricesell = self.try(:PRICESELL) || 0 
    units = self.try(:UNITS) || 0 
    pricesell * units) 
end 

def total 
( 

    price = self.try(:PRICE) || 0 
    units = self.UNITS || 0 
    price * units) 
end 

def consignor_cost 
    cost = product.location.try(:DISCOUNT_CONSIGNOR) || 0 
    cost ? (self.total * cost) : 0 
end 


def cost_of_goods_sold 
cost = product.PRICEBUY || 0 

cost ? (cost * self.TOTALUNITS) : 0 
end 

def gross_profit 
(self.total - self.consignor_cost - self.cost_of_goods_sold) 
end 


class ProductSale < Ticketline 

end 

end 

我控制器

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 

在我想表現出同樣的對蝦產生的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 

它給了我沒有錯誤,但沒有顯示任何值。

什麼是正確的語法?

回答

1

您可以在您的蝦檔案中明確包含ActionView::Helpers::NumberHelper模塊或任何您想要的模塊。

嘗試通過在@grand_total實例變量在PDF文件中的initialize方法:

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 

而在@grand_total在控制器通得過當你創建一個新的PDF對象:

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 

希望這應該工作..

+0

感謝您的回答。但是我不確定在哪裏放置代碼。我已經在我的pdf.rb'高清初始化(product_sales) 超() @product_sales = product_sales 頭 TEXT_CONTENT table_content 頁腳 end'如果我把它放在'高清頁腳 高清初始化(grand_total) super() 文本number_to_currency(grand_total,precision:0,單位:「EUR」,分隔符:「,」) 結束 結束它仍然沒有顯示任何pdf文件。 – Catmal

+0

我也更新了問題,使其更易於閱讀。 – Catmal

+0

你的'@ grand_total'實例變量來自哪裏?它與'@ product_sales'模型​​有關還是它是一個單獨的模型 – Ren