2012-03-13 53 views
0

我正在运行RAils 3.1,ruby 1.9.2应用程序,并且我遇到了CSV问题;我必须从csv文件导入数据;这是我的代码:需要CSV问题

def bulk_operations 
     require 'csv' 
    last_asset = "" 
    last_serial = "" 
    if request.path.include? "ingress_transport_document" 
     transport_document_id = params[:ingress_transport_document_id] 
     td = IngressTransportDocument.find(transport_document_id) 
     ingress = true; 
    elsif request.path.include? "egress_transport_document" 
     transport_document_id = params[:egress_transport_document_id] 
     td = EgressTransportDocument.find(transport_document_id) 
    end 
    if params[:dump_me] 
     begin 

     # @parsed_file=CSV::Reader.parse(params[:dump_me][:file],';',"\n") 

      CSV.foreach(params[:dump_me][:file]) do |row| 

       bw=BulkWarehouse.new 
       bw.serial = row[0] 
       bw.asset = row[1] 
       logger.debug "#{bw.serial},#{bw.asset} --> #{bw.serial.strip}" 
       # 
       # Cerco la presenza di warehouse se la bolla e di tipo aggiornamento 
       # 
       if !bw.serial.strip.empty? && !bw.asset.strip.empty? && update_warehouse 
        warehouse = Warehouse.find_by_serial_and_asset(bw.serial.strip,bw.asset.strip) 
       elsif !bw.serial.strip.empty? && update_warehouse 
        warehouse = Warehouse.find_by_serial(bw.serial.strip) 
       elsif !bw.asset.strip.empty? && update_warehouse 
        warehouse = Warehouse.find_by_asset(bw.asset.strip) 
       end 
       if warehouse && update_warehouse 
        bw.warehouse_id = warehouse.id 
       elsif update_warehouse 
        @wh_errors[:"bulk_warehouse#{n}"] = "Prodotto non presente" 
       end 
       if request.path.include? "ingress_transport_document" 
        bw.ingress_transport_document_id = transport_document_id 
       elsif request.path.include? "egress_transport_document" 
        bw.egress_transport_document_id = transport_document_id 
       end 
       if bw.save 
        n=n+1 
        GC.start if n%50==0 
       end 
       last_serial = row[0] 
       last_asset = row[1] 
      end 
      count = sa_query 
      flash[:notice]="Il CSV e stato importato con successo: #{n} nuovi record sono stati accodati per essere processati.<br>In totale hai #{count} record in attesa di essere processati." 
     #rescue CSV::IllegalFormatError 
      logger.debug("CSV Exception: IllegalFormatError") 
      count = sa_query 
      flash[:notice] = "Il CSV NON sembra essere formattato correttamente." 
      flash[:notice] += "L'ultimo ASSET inserito e stato #{last_asset}." if !last_asset.empty? 
      flash[:notice] += "L'ultimo SERIALE inserito e stato #{last_serial}." if !last_serial.empty? 
      flash[:notice] += "#{n} nuovi record sono stati accodati per essere processati." 
      flash[:notice] += "In totale hai #{count} record in attesa di essere processati." 
     end 
    end 

     # update_warehouse e true solo se la bolla e di tipo da aggiornamento 
      update_warehouse = td.transport_document_type.code == "AGE" || 
           td.transport_document_type.code == "REP" || 
           td.transport_document_type.code == "ASS" 
    if params[:ingress_transport_document_id] || params[:egress_transport_document_id] 
     self.index 
     render :index 
    else 
     redirect_to :action => "index" 
    end 

和Rails给我下面的错误:无法转换ActionDispatch :: HTTP :: UploadedFile的转换成String

任何建议

回答

0
+0

我已更新我的答案;我已经使用CSV.foreach,因为我必须从文件读取而不是从字符串中读取,但是我有另一个错误:( – Marco 2012-03-13 17:26:02

+0

@Marco尝试'CSV.new(params [:dump_me] [:file])。each' – 2012-03-13 18:47:48

+0

非常感谢Austin它的工作原理!我用过:CSV.new(params [:dump_me] [:file] .tempfile, :headers => true, :col_sep =>“,”).each do |行| – Marco 2012-03-13 20:27:49