动态地将行添加到表中

时间:2013-05-24 13:48:08

标签: arrays ruby-on-rails-3 prawn

基于Railscasts的Prawn剧集,我想添加这样的行:

def line_items
  move_down 20
  table line_item_rows do
    row(0).font_style = :bold
    columns(1..3).align = :right
    self.row_colors = ["DDDDDD", "FFFFFF"]
    self.header = true
  end
end

def line_item_rows
  [["Product", "Qty", "Unit Price", "Full Price"]] +
  @order.line_items.map do |item|
    [item.name, item.quantity, price(item.unit_price), price(item.full_price)]
  end
end

我想在“@ order.line_items”行之后动态添加行但我不知道该怎么做,然后我尝试了:

def line_item_rows
  [["Product", "Qty", "Unit Price", "Full Price"]] +
  @order.line_items.map do |item|
    [item.name, item.quantity, price(item.unit_price), price(item.full_price)]
  end

  @order.other_line_items.map do |item|
    [item.name, item.quantity, price(item.unit_price), price(item.full_price)]
  end
end

但当然它不起作用。你知道怎么做吗?

1 个答案:

答案 0 :(得分:0)

解决方案很简单:

data = data + 
  @order.other_line_items.map do |item|
    [item.name, item.quantity, price(item.unit_price), price(item.full_price)]
  end