Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 无法完全导入CSV文件_Ruby_Csv_Ruby On Rails 4 - Fatal编程技术网

Ruby 无法完全导入CSV文件

Ruby 无法完全导入CSV文件,ruby,csv,ruby-on-rails-4,Ruby,Csv,Ruby On Rails 4,我使用此代码将CSV文件导入表: def Hotel.import(file) allowed_attributes = ["id", "name", "area", "short_name", "zip", "address", "tel", "fax", "staff_name", "information", "created_at", "updated_at"] CSV.foreach(file.path, headers: true) do |row| product

我使用此代码将CSV文件导入表:

def Hotel.import(file)
  allowed_attributes = ["id", "name", "area", "short_name", "zip", "address", "tel", "fax", "staff_name", "information", "created_at", "updated_at"]
  CSV.foreach(file.path, headers: true) do |row|
    product = find_by_id(row["id"]) || new
    product.attributes = row.to_hash.select{ |k,v| allowed_attributes.include? k }
    product.save!
  end
end

def import
  Hotel.import(params[:file])
  redirect_to root_url, notice: "Product Imported."
end
然而,这段代码只在表中插入id、创建和更新字段。插入命令显示在此处:

INSERT INTO `hotels` (`created_at`, `updated_at`) VALUES ('2014-04-10 07:50:23', '2014-04-10 07:50:23')

我正在使用rails 4.0和ruby 2.0来完成这个项目。我仔细检查以查找错误,但我在这里找不到任何问题,因此请告诉我哪里错了?

我发现问题在于:当我将文件导出到桌面时,我选择了通过LibreOffice CalcUbuntu 13.10打开它,所以格式错误。我通过gedit文本编辑器打开进行了更改,它工作正常。非常愚蠢的错误

我找不到任何错误。row.to|u hash.select{k,v|allowed_attributes.include?k}返回什么?您的强参数中是否实际允许允许允许允许允许允许允许允许允许允许的_属性?@Stefan原始代码是“*accessible_atributes”,而不是row.to|hash.select{k,v|allowed_attributes.include?k},但是此原始代码在rails 4中导致错误,然后我搜索并找到了这个解决方案。这段代码让我感到困惑。插入p行。to|hash。选择{k,v| allowed_attributes.include?k},检查对象并打印结果,您应该在日志中看到它我发布p行的行。to|hash。。。应该打印一个散列,类似于{id=>1,…}