Rails prawnto gem从multiple-db生成pdf

时间:2012-12-06 12:51:39

标签: ruby-on-rails pdf model prawn prawnto

我有这样的模特:

class WorkOrder < ActiveRecord::Base
  self.table_name = "work_orders"
  set_primary_key "id"
  has_many :work_order_details
  belongs_to :type
  belongs_to :client
def get_action_name(id)
    n = Action.find(id)
    logger.warn("!!!!!!!!!!!!!!! AAAA !!!!!!!!!!!!!!!!!!!")
    logger.warn(n.name)
    n.name
  end

  def get_price_name(id)
    n = ActionDetail.find_by_action_id(id)
    logger.warn("!!!!!!!!!!!!!!! AAAA !!!!!!!!!!!!!!!!!!!")
    n.price
  end

  def get_master_name(id)
    n = Master.find(id)
    logger.warn("!!!!!!!!!!!!!!! AAAA !!!!!!!!!!!!!!!!!!!")
    a = n.lastname + " " + n.firstname
    a
  end
end



class WorkOrderDetail < ActiveRecord::Base
  self.table_name = "work_order_details"
  set_primary_key "id"
  belongs_to :master
  belongs_to :action
  belongs_to :work_order
end



class Action < ActiveRecord::Base
  self.table_name = "actions"
  set_primary_key "id"
  belongs_to :specialization
  has_many :action_details
  has_many :work_order_details
end



class Master < ActiveRecord::Base
  self.table_name = "masters"
  set_primary_key "id"
  has_many :master_specializations
  has_many :work_order_details
end

我必须生成类似视图的pdf

%table.zebra
  %tr
    %th id
    %th Услуга
    %th Мастер
    %th Стоимость
    %th Бонус
    %th
    %th
    %th
  -if @work_order.work_order_details.kind_of?(Array)
    - @work_order.work_order_details.each do |work_order_detail|
      %tr{:class => cycle("zebra-stripe zebra-grey zebra1", "zebra-stripe zebra-grey zebra2")}
        %td= work_order_detail.work_order_id
        %td= work_order_detail.get_action_name(work_order_detail.action_id)
        %td= work_order_detail.get_master_name(work_order_detail.master_id)
        %td= work_order_detail.get_price_name(work_order_detail.action_id)
        - count = count + work_order_detail.get_price_name(work_order_detail.action_id)
        %td= work_order_detail.bonus
    Общая сумма:
    = count
  -else
    %tr{:class => cycle("zebra-stripe zebra-grey zebra1", "zebra-stripe zebra-grey zebra2")}
      %td= @work_order.work_order_details.work_order_id
      %td=# @work_order.work_order_details.get_action_name(@work_order.work_order_details.action_id)
      %td= @work_order.work_order_details.master_id
      %td= @work_order.work_order_details.bonus
  = link_to 'Добавить услугу', new_work_order_detail_path   

它的工作没问题,但是当我尝试这样做时:

pdf.font_families.update(
      "Verdana" => {
        :bold => "C:/verdanab.ttf",
        :italic => "C:/verdanai.ttf",
        :normal  => "C:/verdana.ttf" })
    pdf.font "Verdana", :size => 10
pdf.text "Заказ-наряд ##{@work_order.id}", :size => 20, :style => :bold
pdf.text "##{@work_order.date_of_work_start}", :size => 8, :style => :bold

pdf.move_down(30)




 items = @work_order.work_order_details.map do |item|
  [   
    item.work_order_id
    item.get_action_name(item.action_id)
    item.get_master_name(item.master_id)
    item.get_price_name(item.action_id)
    item.bonus
  ]
end



pdf.table items, :border_style => :grid,
  :row_colors => ["FFFFFF","DDDDDD"],
  :headers => ["id", "Услуга", "Мастер", "Стоимость"]


pdf.move_down(10)

它给了我错误......

控制器的方法:

def show
    @work_order = WorkOrder.find(params[:id])
    @work_order_details = WorkOrderDetail.find_all_by_work_order_id(@work_order.id)
    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @work_order }
      format.pdf { render :layout => false }
    end
  end

错误的:

C:/_Projects/CarService/app/views/work_orders/show.pdf.prawn:18: syntax error, unexpected tIDENTIFIER, expecting ']'
    item.get_action_name(item.action_id)
        ^
C:/_Projects/CarService/app/views/work_orders/show.pdf.prawn:22: syntax error, unexpected ']', expecting keyword_end
C:/_Projects/CarService/app/views/work_orders/show.pdf.prawn:34: syntax error, unexpected keyword_ensure, expecting keyword_end
C:/_Projects/CarService/app/views/work_orders/show.pdf.prawn:36: syntax error, unexpected $end, expecting keyword_end

那么如何用另一个表中的数据来做pdf?

当我写

@work_order.work_order_details.each do |item|  
  pdf.text item.get_action_name(item.action_id)
end  

一切都好吗

1 个答案:

答案 0 :(得分:0)

HEH) 看看

items = @work_order.work_order_details.map do |item|
  [   
    item.work_order_id
    item.get_action_name(item.action_id)
    item.get_master_name(item.master_id)
    item.get_price_name(item.action_id)
    item.bonus
  ]
end

我忘了逗号。用:

 items = @work_order.work_order_details.map do |item|
  [   
    item.work_order_id,
    item.get_action_name(item.action_id),
    item.get_master_name(item.master_id),
    item.get_price_name(item.action_id),
    item.bonus
  ]
end

一切都好。)