我正在向在线订购应用中添加导入CSV,之前的问题包括ROR import csv file no method error,Adding CSV Import to a ROR application。感谢那些在其他问题上提供帮助的人。
错误:
NoMethodError (undefined method `build_from_csv' for #<Class:0x9ddd82c>):
app/controllers/imports_controller.rb:14:in `block in imports'
/usr/local/ruby/lib/ruby/1.9.1/csv.rb:1761:in `each'
/usr/local/ruby/lib/ruby/1.9.1/csv.rb:1370:in `parse'
app/controllers/imports_controller.rb:10:in `imports'
imports_controller:
require 'csv'
class ImportsController < ApplicationController
def imports
logger.info "File loaded"
infile = params[:dump][:infile].read
n, errors = 0, []
@archive = []
CSV.parse(infile) do |row|
n += 1
# SKIP: header i.e. first row OR blank row
next if n == 1 or row.join.blank?
imports = Order.build_from_csv(row)
if imports.valid?
imports.save
@archive << row
else
errors << row
end
end
logger.info errors
flash[:success] = "The CSV imported successfully"
respond_to do |format|
format.html # index.html.erb
format.json { render :json => @archive }
end
end
最初我有一个models / imports.rb,但我把它移到models / order.rb,因为我试图将csv日期添加到该表:
def self.build_from_csv(row)
#last_level = (row[5].downcase == "yes"
orders = Orders.new(:imports => "#{row[0]}}",
:cart_items => "#{row[1]}",
:cart_items_quantity => "#{row[2]}",
:cart_items_price => "#{row[3]}",
:cart_items_description => "#{row[4]}",
:cart_items_upc => "#{row[5]}",
:cart_items_sku => "#{row[6]}")
# :last_level=> last_level)
return imports
end
表示错误行:
imports = Order.build_from_csv(row)
我试过了
imports = Imports.build_from_csv(row)
我使用了Order,因为那是购物车项目插入但没有运气的表格。我也尝试过:
imports = CSV.build_from_csv(row)
...
imports = Infile.build_from_csv(row)
并且导致了未初始化的常量。我愿意接受其他尝试的建议,再次感谢您的帮助!