2012-03-13 6 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は私に次のエラーを与える:それはちょうどCSV.parseだRuby 1.9の内の任意の提案に文字列

答えて

0

をActionDispatch ::のHttp ::にUploadedFileを変換することはできません。参照してください:http://www.ruby-doc.org/stdlib-1.9.3/libdoc/csv/rdoc/CSV.html#method-c-parse

+0

私は私の答えを更新しました。私は文字列ではなくファイルから読み込む必要があるのでCSV.foreachを使用しましたが、別のエラーがあります:( – Marco

+0

@Marco Try 'CSV.new(params [:dump_me] [:file])。each' –

+0

オースティンは大変ありがとうございます!CSV.new(params [:dump_me] [:file] .tempfile、 :headers => true、 :col_sep => "、").each do |行| – Marco

関連する問題