[英]Ruby on Rails 4 - CSV import - no implicit conversion into string
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
You need to do as : 你需要这样做:
CSV.foreach(file.path)
Your file
is holding an instance of ActionDispatch::Http::UploadedFile
class. 您的
file
正在保存ActionDispatch::Http::UploadedFile
类的实例。 To know the path, use #path
method. 要知道路径,请使用
#path
方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.