我正在尝试使用 FasterCSV 导入制表符分隔的文件。我尝试了各种各样的事情并得到了不同的错误。在当前状态下,我收到“未定义的方法 `tempfile'”错误。
我已在我的创建操作中添加了 fastercsv 代码,因为批量导入是将数据添加到此模型的唯一方式。
这是我的代码。有人可以帮忙吗?任何帮助将不胜感激!
我的模型:
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