0

嗨,我已经使用 Prawn 在我的 Rails 应用程序中创建了一个 PDF 转换,它工作正常。现在我在电子邮件附件中发送该 PDf。现在的问题是,如果我不使用任何辅助方法,我可以发送 PDF 附件,但是当我在 PDf 文件中使用我的 format_currency 方法时,它会在 instance_eval 方法上给出错误。这是我的代码示例:格式货币代码:

module ApplicationHelper

def format_currency(amt)
    unit = 'INR'
    country = current_company.country
    if !country.blank? && !country.currency_unicode.blank?
      unit = country.currency_unicode.to_s
    elsif !country.blank? 
      unit = country.currency_code.to_s
    end
    number_to_currency(amt, :unit => unit+" ", :precision=> 2)   
  end 
end 

我的控制器代码:

pdf.instance_eval do
      @company = current_company
      @invoice = invoice
      @invoice_line_items = invoice_line_items
      @receipt_vouchers = receipt_vouchers
      eval(template) #this evaluates the template with your variables
    end

我得到的错误信息:

undefined method `format_currency' for #<Prawn::Document:0x7f89601c2b68>

使用此代码,如果我不使用辅助方法但我需要使用该方法,我可以成功发送附件。

4

1 回答 1

0

我以不同的方式解决了这个问题,我在我的公司模型中创建了一个新的 currency_code 方法,并在我的 format_currency 辅助方法中调用了它,它对我来说很好:这是我所做的:

def currency_code
  unit = 'INR'
  if !country.blank? && !country.currency_unicode.blank?
      unit = country.currency_unicode.to_s
  elsif !country.blank? 
      unit = country.currency_code.to_s
  end
   unit
end

并在我的 format_currency 助手中使用它:

def format_currency(amt)
    unit = current_company.currency_code
    number_to_currency(amt, :unit => unit+" ", :precision=> 2)   
  end 

在我的控制器中,我添加了一个货币变量并在我的 pdf 文件中使用它:

 pdf.instance_eval do
      @company = current_company
      @invoice = invoice
      @invoice_line_items = invoice_line_items
      @receipt_vouchers = receipt_vouchers
      @currency = current_company.currency_code # new added variable for currency
      eval(template) #this evaluates the template with your variables
    end
于 2013-01-11T06:46:49.343 回答