【问题标题】:Require CSV Issue需要 CSV 问题
【发布时间】:2012-03-13 16:51:28
【问题描述】:

我正在运行 RAils 3.1、ruby 1.9.2 应用程序,但我遇到了 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 给我以下错误:can't convert ActionDispatch::Http::UploadedFile into String

任何建议

【问题讨论】:

    标签: ruby-on-rails-3 csv


    【解决方案1】:

    【讨论】:

    • 我已经更新了我的答案;我使用了 CSV.foreach,因为我必须从文件中读取,而不是从字符串中读取,但我还有另一个错误:(
    • @Marco Try CSV.new(params[:dump_me][:file]).each
    • 非常感谢奥斯汀它有效!我用过: CSV.new( params[:dump_me][:file].tempfile, :headers => true, :col_sep=> "," ).each do |row|
    猜你喜欢
    • 1970-01-01
    • 2010-12-13
    • 2023-03-25
    • 1970-01-01
    • 2012-08-05
    • 2018-05-29
    • 2014-09-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多