[英]Ruby on Rails 4 - CSV import - no implicit conversion into string
我正在嘗試將CSV數據導入我的mysql數據庫,但是當我嘗試上傳文件時遇到錯誤:
no implicit conversion of ActionDispatch::Http::UploadedFile into String
這就是CSV文件中的一行:
751,"01/17/2015","11:17:32","60","TDFSRDSK","2","10","-1","0","3","","26","3","","","1","0"
這是導入的代碼,在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
在product_controller.rb中
def import
Product.import(params[:file])
redirect_to_root_url, notice "Products imported"
end
在索引中:
<%= form_tag import_products_path, multipart: true do %>
<%= file_field_tag :file %>
<%= submit_tag "Import" %>
<% end %>
在routes.rb我有:
resources :products do
collection {post :import}
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.