基本上,CSV 导入不起作用。
型号代码:
def self.import(file,admin_id)
spreadsheet = open_spreadsheet(file)
header = spreadsheet.row(1)
(2..spreadsheet.last_row).each do |i|
row = Hash[[header, spreadsheet.row(i)].transpose]
new_row = row.to_hash.merge(:merchant_id => admin_id)
Inventory.create! new_row
end
end
def self.open_spreadsheet(file)
require 'iconv'
case File.extname(file.original_filename)
when ".csv" then Roo::Csv.new(file.path, nil, :ignore)
when ".xls" then Roo::Excel.new(file.path, nil, :ignore)
when ".xlsx" then Roo::Excelx.new(file.path, nil, :ignore)
else raise "Unknown file type: #{file.original_filename}"
end
end
控制器动作:
def import
Inventory.import(params[:file],params[:admin_id])
redirect_to :inventories, :notice => "Imported Successfully."
end
导入按钮,选择文件,一切正常。提交时:
SyntaxError in InventoriesController#import (eval):2: syntax error, unexpected tIDENTIFIER, expecting ')' read_key(:Parts name) ^ (eval):6: syntax error, unexpected tIDENTIFIER, expecting ')' read_key_before_type_cast(:Parts name) ^ (eval):10: syntax error, unexpected tIDENTIFIER, expecting ')' write_key(:Parts name, value) ^ (eval):10: syntax error, unexpected ')', expecting '=' (eval):14: syntax error, unexpected tIDENTIFIER, expecting ')' read_key(:Parts name).present? ^ (eval):15: syntax error, unexpected $end, expecting keyword_end
有人可以对此有所了解吗?我什至找不到调试实际方法的好方法,以准确查看它试图做什么以及它是如何失败的。
有一个模板 CSV 文件,在标题行中看起来像这样:
零件名称 零件编号 数量 成本 价格 税 位置 描述 可编辑