【问题标题】:NoMethodError using Prawn and Rails Casts as referenceNoMethodError 使用 Prawn 和 Rails Casts 作为参考
【发布时间】:2014-01-28 03:55:20
【问题描述】:

我正在使用 Prawn 0.14.0

我正在使用 rails casts #153 Pdf With Prawn Revised 作为参考来生成包含一些信息和表格的 pdf。但我收到此错误:

LendingsController#show 中的 NoMethodError - 未定义方法 `line_items'

如果我没有在“line_items 方法”中调用方法“line_item_rows”,我可以生成 pdf 并显示一些信息但是如果我尝试填充调用 line_item_rows 的表,则会收到上述错误。

我的 TermPdf 课程中有这个:

class TermPdf < Prawn::Document

def initialize(lending)
    super() 
    @lending_term = lending
    line_items
end


def line_items
    move_down 20
    table line_item_rows       
end  

def line_item_rows
  [["Equipament","Brand","Tipping","Description"]] + 
    @lending_term.line_items.map do |item|
       [
         item.equipment.title,
         item.equipment.brand.title,
         item.equipment.tipping,
         item.description
       ]
    end  
end

结束

这是 LendingsController 中的 show 方法

def show
   @lending = Lending.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
         format.pdf do        
           pdf = TermPdf.new(@lending)
           send_data pdf.render, filename: "Agreement Number #{@lending.id}.pdf",
                          type: "application/pdf",
                          disposition: 'inline' 
       end  
      format.json { render json: @lending }
    end
end

我确实尝试过修复这个错误但没有成功。

【问题讨论】:

    标签: ruby-on-rails-3 prawn


    【解决方案1】:

    问题似乎来自这里:

    @lending_term.line_items
    

    现在我不知道这是一个关系还是一个类方法,但你需要检查一下!! PDF 代码对我来说似乎很干净。

    这里的一个小建议是避免在代码中出现模棱两可的命名。您正在寻找错误的方向,因为您在不同的情况下使用了名称“Line_items”,这使得调试更加困难。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-12
      • 2010-12-27
      相关资源
      最近更新 更多