使用 Prawn 使用属于 collectionproxy 的值数组创建 PDF [英] Create PDF with array of values belonging to collectionproxy using Prawn

查看:42
本文介绍了使用 Prawn 使用属于 collectionproxy 的值数组创建 PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Ruby on Rails 4

Ruby on Rails 4

创建包含问题和答案的 PDF.问题创建得很好.现在我正在尝试将相关答案放在每个问题的旁边或下方.

Creating a PDF with questions and answers. The questions are created fine. Now I am trying to put the related answers next to or below each question.

我可以为每个问题显示一个答案,现在我遇到了问题,因为答案存储在一个数组中.

I am able to show one answer per question, now I am having a problem because the answers are stored in an Array.

控制器:

def show
  @all_answers = []
  @test = Test.find(params[:id])
  @test_questions = @test.questions
  @test_questions.each do |a|
    @all_answers << Answer.find_all_by_question_id(a)
  end
  respond_to do |format|
    format.html
    format.pdf do
      pdf = TestReport.new(@test_questions, @all_answers)
      send_data pdf.render, filename: 'certification_test.pdf', type: 'application/pdf' , disposition: 'inline' 
    end
  end
end

PDF 类代码:

class TestReport < LayoutPdf

def initialize(test=[], answer=[])
  super()
  @test_questions = test
  @all_answers = answer

  header 'Certification Test'
  display_test
  footer

end

def display_test
  table test_questions do
    self.header = true
    self.column_widths = [40, 300, 200]
  end  
end

def test_questions
  [['#', 'Question']] +
  @test_me ||= @test_questions.map { |e| [e.id, e.content] } +
  #@all_answers.each do |a| a.content end
  @answer_me ||= @all_answers.map { |a| [a.content] }
end
end

@all_answers 是一个这样的数组:

@all_answers is an array like this:

[[#<Answer id: 1, content: "It walked", question_id: 1, correct: false, created_at: "x", updated_at: "x">, #<Answer id: 2, content: "It was thrown", question_id: 1, correct: true, created_at: "x", updated_at: "x">, #<Answer id: 3, content: "It got run over and pushed", question_id: 1, correct: false, created_at: "x", updated_at: "x">], [#<Answer id: 4, content: "False", question_id: 2, correct: true, created_at: "x", updated_at: "x">, #<Answer id: 5, content: "True", question_id: 2, correct: false, created_at: "x", updated_at: "x">]]

我为这一行得到的错误:@answer_me ||= @all_answers.map { |a|a.内容 }

The error I get for this line: @answer_me ||= @all_answers.map { |a| a.content }

undefined method `content' for #<Array:0x007f2cbac9e620>

推荐答案

@all_answers 是一个数组数组.您应该在迭代之前将其展平:

@all_answers is an array of arrays. You should flatten it before iterating over it:

@answer_me ||= @all_answers.flatten.map { |a| a.content }

这篇关于使用 Prawn 使用属于 collectionproxy 的值数组创建 PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆