I'm trying to import CSV data to my mysql DB but I'm running into an error when I try to upload the file :
我正在尝试将CSV数据导入我的mysql数据库,但是当我尝试上传文件时遇到错误:
no implicit conversion of ActionDispatch::Http::UploadedFile into String
This is what a line in the CSV file looks like :
这就是CSV文件中的一行:
751,"01/17/2015","11:17:32","60","TDFSRDSK","2","10","-1","0","3","","26","3","","","1","0"
here is the code for the import, in product.rb
这是导入的代码,在product.rb中
def self.import(file)
CSV.foreach(file) do |row|
id, jour, heure, valeur, app, a, b, c, d, e, f, g, h, i, j, k, l = row
userid = current_user.id
product = Product.create(date: jour, valeur: valeur,user_id: userid)
end
end
in product_controller.rb
在product_controller.rb中
def import
Product.import(params[:file])
redirect_to_root_url, notice "Products imported"
end
in the index :
在索引中:
<%= form_tag import_products_path, multipart: true do %>
<%= file_field_tag :file %>
<%= submit_tag "Import" %>
<% end %>
and in routes.rb I have:
在routes.rb我有:
resources :products do
collection {post :import}
end
2 个解决方案
#1
14
Try give params[:file].path
in argument. Like this,
尝试在参数中给出params [:file] .path。喜欢这个,
Product.import(params[:file].path)
Checkout this.
看看这个。
#2
2
You need to do as :
你需要这样做:
CSV.foreach(file.path)
Your file
is holding an instance of ActionDispatch::Http::UploadedFile
class. To know the path, use #path
method.
您的文件正在保存ActionDispatch :: Http :: UploadedFile类的实例。要知道路径,请使用#path方法。
#1
14
Try give params[:file].path
in argument. Like this,
尝试在参数中给出params [:file] .path。喜欢这个,
Product.import(params[:file].path)
Checkout this.
看看这个。
#2
2
You need to do as :
你需要这样做:
CSV.foreach(file.path)
Your file
is holding an instance of ActionDispatch::Http::UploadedFile
class. To know the path, use #path
method.
您的文件正在保存ActionDispatch :: Http :: UploadedFile类的实例。要知道路径,请使用#path方法。