使用Prawn gem进行pdf生成的Ruby on rails应用程序在渲染时出错:nil的未定义方法`text':NilClass

时间:2011-12-14 13:18:41

标签: ruby-on-rails-3 prawn

控制器中的

代码

def download_report
  @downloads  = StatisticDownload.select("date(Date) as downloaded_date, count(id) as count").where("DownloadSuccess=?","1").group("date(Date)")
  respond_to do |format|
    format.pdf { @downloads }
  end
end

创建视图

# download_report.pdf.prawn
pdf.text "Download ##{@downloads.id}", :size => 30, :style => :bold

downloads = @downloads.map do |downloads|
  [
    downloads.file,
    downloads.id
  ]
end

但../generate_report.pdf会生成错误: 当你没有期待它时,你有一个零对象! 您可能期望一个Array实例。 评估nil.map时发生错误

我已经安装了0.12.0 prawn和prawnto。

2 个答案:

答案 0 :(得分:2)

为此,请参阅http://www.idyllic-software.com/blog/creating-pdf-using-prawn-in-ruby-on-rails/

你可以找到简单的解决方案..

答案 1 :(得分:0)

尝试删除整个respond_to块,以便您拥有:

def download_report
  @downloads  = StatisticDownload.select("date(Date) as downloaded_date, count(id) as count").where("DownloadSuccess=?","1").group("date(Date)")
end

或者,如果您确实需要respond_to块,请不要在format.pdf后指定一个块:

def download_report
  @downloads  = StatisticDownload.select("date(Date) as downloaded_date, count(id) as count").where("DownloadSuccess=?","1").group("date(Date)")
  respond_to do |format|
    format.pdf
  end
end

在这两种情况下,我猜你需要让prawnto的控制器魔法接管。