Ruby on Rails 4 - import CSV - implicit conversion to string

I am trying to import CSV data into my mysql database, but when trying to upload a file, I encountered an error:

no implicit conversion of ActionDispatch::Http::UploadedFile into String 

Here is the line in the CSV file:

 751,"01/17/2015","11:17:32","60","TDFSRDSK","2","10","-1","0","3","","26","3","","","1","0" 

here is the code to import, in 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

 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:

 resources :products do collection {post :import} end 
+6
source share
2 answers

Try giving the argument params[:file].path . Like this,

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

Checkout this .

+14
source

You need to do the following:

 CSV.foreach(file.path) 

Your file contains an instance of the ActionDispatch::Http::UploadedFile class. To find out the path, use the #path method.

+2
source

Source: https://habr.com/ru/post/987737/


All Articles