1

我有这样的模型:

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时:

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  

一切都好

4

1 回答 1

0

呵呵)看看

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

一切正常)

于 2012-12-06T16:44:01.803 回答