簡體   English   中英

使用FasterCSV / Rails 3.1 / Ruby 1.8.7導入

[英]Import using FasterCSV / Rails 3.1 / Ruby 1.8.7

我正在嘗試使用FasterCSV導入制表符分隔的文件。 我嘗試了各種事情,並得到各種錯誤。 在當前狀態下,我收到“未定義的方法'tempfile'”錯誤。

我已將fastcsv代碼添加到我的create動作中,因為批量導入是將數據添加到此模型的唯一方法。

這是我的代碼。 有人可以幫忙嗎? 任何幫助將不勝感激!

我的模特:

class AppleSale < ActiveRecord::Base
end

我的控制器:

require 'fastercsv'
require 'tempfile'

class AppleSalesController < ApplicationController

  def new
    @apple_sale = AppleSale.new
    respond_to do |format|
      format.html # new.html.erb
      format.json { render :json => @apple_sale }
    end
  end

 def create
   file = params[:tsv_file] 
   FasterCSV.new(file.tempfile, {:headers => true, :quote_char=>'"', :col_sep =>"\t"}) do |row_data|

        new_record = AppleSale.first_or_new(
            'provider' =>  row_data['provider'],
            'provider_country' => row_data['provider_country'],
            'vendor_identifier' => row_data['vendor_identifier'],
            'upc' => row_data['upc'],
            'isrc' => row_data['isrc'],
            'artist_show' => row_data['artist_show'],
            'title' => row_data['title'],
            'label_studio_network' => row_data['label_studio_network'],
            'product_type_identifier' => row_data['product_type_identifier'],
            'units' => row_data['units'],
            'royalty_price' => row_data['royalty_price'],                    
            'download_date' => row_data['download_date'],
            'order_id' => row_data['order_id'],
            'postal_code' => row_data['postal_code'],
            'customer_identifier' => row_data['customer_identifier'],
            'report_date' => row_data['report_date'],
            'sale_return' => row_data['sale_return'],
            'customer_currency' => row_data['customer_currency'],
            'country_code' => row_data['country_code'],
            'royalty_currency' => row_data['royalty_currency'],
            'preorder' => row_data['preorder'],
            'isan' => row_data['isan'],
            'customer_price' => row_data['customer_price'],
            'apple_identifier' => row_data['apple_identifier'],
            'cma' => row_data['cma'],
            'asset_content_flavor' => row_data['asset_content_flavor'],
            'vendor_order_code' => row_data['vendor_order_code'],
            'grid' => row_data['grid'],
            'promo_code' => row_data['promo_code'],
            'parent_identifier' => row_data['parent_identifier'],
            'apple_identifier' => row_data['apple_identifier']                 
        )
        new_record.save
    end
  end
end

我的表單視圖:

<%= form_for(@apple_sale, :multipart => true) do |f| -%>
<%= f.file_field :tsv_file %>
<%= f.submit "Upload >>", :class => "submit"  %>
<% end %>

我的Gemfile包含:

gem 'fastercsv'

提前致謝!!

其他可能需要更新的人,我無法回答自己的問題:

一切都在控制器中,更改為以下內容。 這似乎完美地工作。

def create
   uploaded_io = params[:apple_sale][:tsv_file] 
   File.open(Rails.root.join('public', 'uploads', uploaded_io.original_filename), 'w') do |file|
   file.write(uploaded_io.read)
  end

   FasterCSV.foreach(uploaded_io.original_filename, {:headers => true, :col_sep =>"\t"}) do |row_data|

        new_record = AppleSale.new(
            'provider' =>  row_data[0],
            'provider_country' => row_data[1],
            'vendor_identifier' => row_data[2],
            'upc' => row_data[3],
            'isrc' => row_data[4],
            'artist_show' => row_data[5],
            'title' => row_data[6],
            'label_studio_network' => row_data[7],
            'product_type_identifier' => row_data[8],
            'units' => row_data[9],
            'royalty_price' => row_data[10],                    
            'download_date' => row_data[11],
            'order_id' => row_data[12],
            'postal_code' => row_data[13],
            'customer_identifier' => row_data[14],
            'report_date' => row_data[15],
            'sale_return' => row_data[16],
            'customer_currency' => row_data[17],
            'country_code' => row_data[18],
            'royalty_currency' => row_data[19],
            'preorder' => row_data[20],
            'isan' => row_data[21],
            'customer_price' => row_data[22],
            'apple_identifier' => row_data[23],
            'cma' => row_data[24],
            'asset_content_flavor' => row_data[25],
            'vendor_order_code' => row_data[26],
            'grid' => row_data[27],
            'promo_code' => row_data[28],
            'parent_identifier' => row_data[29]

        )
        new_record.save
    end

        respond_to do |format|
          format.html { redirect_to apple_sales_path, :notice => "Successfully imported sales." }   
        end

  end  

1 :我不認為FasterCSV接受帶有new的塊。

2 :根據Rails 3.1文檔-當您實現文件上傳表單時:
http://guides.rubyonrails.org/form_helpers.html#uploading-files

...產生的paramIO對象, 不一定是普通文件

查看FasterCSV源,看起來好像parse接受了IO對象+一個塊,所以我認為應該這樣做:

FasterCSV.parse(file, ...) do |row_data|
  ...
end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM