【问题标题】:Ruby, Prawn PDF Class, each_with_indexRuby,Prawn PDF 类,each_with_index
【发布时间】:2015-05-08 01:21:09
【问题描述】:

红宝石 2.1.2 导轨 4.1.1

我的 Prawn PDF 课程中有一个表格,并且我有一个像这样工作的版本...

def rows
  [['ITEM', 'DESCRIPTION']] +
  @rows.map do |row|
    ["", row.description]
  end
end

我需要对“ITEMS”字段进行编号/加一。所以第一行是 1,第二行是 2 等等。

我正在尝试像这样使用“each_with_index”...

def rows
  [['ITEM', 'DESCRIPTION']] +
  @rows.each_with_index do |row, index|
    indexvalue = index + 1
    [indexvalue, row.description]
  end
end

但我收到以下错误“数据必须是可单元格对象的二维数组”。我是否正确使用 each_with_index 还是有其他方法可以解决?

【问题讨论】:

    标签: increment prawn ruby-on-rails-4.1 ruby-2.1


    【解决方案1】:

    可能是更好的方法,但这里是工作代码...

    def rows
      index = 0
      [['ITEM', 'DESCRIPTION']] +
      @rows.map do |row|
        index += 1
        cell_1 = index
        cell_2 = row.description
        [cell_1, cell_2]
      end
    end
    

    【讨论】:

    • Post 应该被称为“Rails, Prawn, increment table rows”之类的。无论如何...
    猜你喜欢
    • 1970-01-01
    • 2016-02-23
    • 1970-01-01
    • 2011-08-04
    • 1970-01-01
    • 2016-01-31
    • 1970-01-01
    • 2012-06-04
    • 1970-01-01
    相关资源
    最近更新 更多