2012-03-14 24 views
7

Tôi đang cố gắng sử dụng rails 3.2 giúp việc bên trong một lớp prawn, nhưng đường ray ném:Rails/Prawn: Tôi làm cách nào để sử dụng những người trợ giúp đường ray trong lớp học Prawn?

undefined method `number_with_precision' for #<QuotePdf:0x83d4188> 

Tôm Lớp

class QuotePdf < Prawn::Document 
    def initialize(quote) 
    super() 

    text "sum: #{number_with_precision(quote.sum)}" 
    end 
end 

khiển

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 

Cảm ơn sự giúp đỡ của bạn.

Trả lời

11

Bạn phải bao gồm rõ ràng ActionView::Helpers::NumberHelper (hoặc bất kỳ lớp học/mô-đun trợ giúp nào khác) trong lớp tài liệu tôm của bạn.

class QuotePdf < Prawn::Document 
    include ActionView::Helpers::NumberHelper # <- 

    def initialize(quote) 
    super() 

    text "sum: #{number_with_precision(quote.sum)}" 
    end 
end 
+0

Làm việc tốt cho tôi trong Rails 3.2.11. Siekfried thì không. Cảm ơn! –

+0

bao gồm phải ở bên ngoài lớp (trước khi định nghĩa lớp) trên phiên bản đường ray mới nhất !! – mArtinko5MB

5

Nếu giải pháp iafonov không hoạt động, bạn có thể chỉ cần bao gồm NumberHelper không có tiền tố.

6

Chỉ cần vượt qua view_context đến bộ khởi tạo lớp con Prawn.

def initialize(quote, view_context) 
    super() 
    @view = view_context 
end 

trong Controller, thay đổi để:

QuotePdf.new(@quote, view_context) 

sau đó trong Tôm sub-class, điều này sẽ làm việc:

@view.number_with_precision(quote.sum) 
+0

Tôi thích phương pháp này tốt hơn nhiều, vì nó giữ @view và các phương pháp riêng biệt. Bằng cách này bạn sẽ không bao giờ có một cái gì đó từ ActionView vô tình monkeypatch một cái gì đó trong Prawn :: Tài liệu. – sockmonk

Các vấn đề liên quan