Ruby on rails CSV.foreach可以使用txt文件,但不能使用CSV文件
当我在Ruby on Rails应用程序中导入CSV文件时,它会抛出一个错误。但是当我把它转换成一个.txt文件时,它就工作了 我的模型:Ruby on rails CSV.foreach可以使用txt文件,但不能使用CSV文件,ruby-on-rails,csv,Ruby On Rails,Csv,当我在Ruby on Rails应用程序中导入CSV文件时,它会抛出一个错误。但是当我把它转换成一个.txt文件时,它就工作了 我的模型: class Zip < ActiveRecord::Base require 'csv' def self.import(file) CSV.foreach(file.path, headers: true) do |row| zip_hash = row.to_hash puts "=============
class Zip < ActiveRecord::Base
require 'csv'
def self.import(file)
CSV.foreach(file.path, headers: true) do |row|
zip_hash = row.to_hash
puts "==============\n\n"
puts zip_hash // Just to confirm the hash was created
// {"ZIP"=>"00501", "CBSA"=>"35004", "RES_RATIO"=>"0.000000000", "BUS_RATIO"=>"1.000000000", "OTH_RATIO"=>"0.000000000", "TOT_RATIO"=>"1.000000000"}
puts "==============\n\n"
Zip.create!(zip_hash)
end
end
end
zips
表的数据库模式:
ActiveRecord::Schema.define(version: 2018_06_19_024458) do
create_table "zips", force: :cascade do |t|
t.integer "ZIP"
t.integer "CBSA"
t.decimal "RES_RATIO"
t.decimal "BUS_RATIO"
t.decimal "OTH_RATIO"
t.decimal "TOT_RATIO"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
end
我做错了什么
以下是一些屏幕截图:
你能添加
放入zip_哈希的结果吗
什么是csv内容,zip_哈希需要以参数格式显示更多的错误堆栈跟踪。你也能发布zip表的DB模式吗?@Farid请发布更多错误日志和控制器代码,以便我们能够深入实际问题。
class CreateZips < ActiveRecord::Migration[5.2]
def change
create_table :zips do |t|
t.integer :ZIP
t.integer :CBSA
t.decimal :RES_RATIO
t.decimal :BUS_RATIO
t.decimal :OTH_RATIO
t.decimal :TOT_RATIO
t.timestamps
end
end
end
ActiveRecord::Schema.define(version: 2018_06_19_024458) do
create_table "zips", force: :cascade do |t|
t.integer "ZIP"
t.integer "CBSA"
t.decimal "RES_RATIO"
t.decimal "BUS_RATIO"
t.decimal "OTH_RATIO"
t.decimal "TOT_RATIO"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
end