无法完全导入CSV文件

时间:2014-04-10 07:59:59

标签: 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 = 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,created_at和updated_at字段。 insert命令显示在这里:

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来完成这个项目。我仔细检查发现错误,但我在这里找不到任何问题,所以请告诉我哪里出错了?

1 个答案:

答案 0 :(得分:0)

我发现问题是:当我将文件导出到桌面时,我选择通过LibreOffice Calc(Ubuntu 13.10)打开它,因此格式错误。我通过gedit文本编辑器打开更改,它工作正常。非常愚蠢的错误!

相关问题