簡體   English   中英

Ruby on Rails 4 - CSV導入 - 沒有隱式轉換為字符串

[英]Ruby on Rails 4 - CSV import - no implicit conversion into string

我正在嘗試將CS​​V數據導入我的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

嘗試在參數中給出params[:file].path 像這樣,

Product.import(params[:file].path)

結帳這個

你需要這樣做:

CSV.foreach(file.path)

您的file正在保存ActionDispatch::Http::UploadedFile類的實例。 要知道路徑,請使用#path方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM