需要CSV问题

时间:2012-03-13 16:51:29

标签: ruby-on-rails-3 csv

我正在运行RAils 3.1,ruby 1.9.2 app并且我遇到了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

任何建议

1 个答案:

答案 0 :(得分:0)