For my application I am uploading a file via file_field_tag
. How do I use that file with SmarterCSV? I have tried something like this SmarterCSV.process(@file.tempfile.path)
and it just returns an empty array even though there is data in the CSV.
Any thoughts?
The proposed answer does not work.
If you however do it like this:
clients_data = params[:file_categories]
clients = SmarterCSV.process(clients_data.tempfile, {:chunk_size => 1, :key_mapping => {:ca_id => :id, :ca_desc => :name}})
This works fine. You need to use tempfile instead of original_filename.
It's not clear what @file is, but if from a typical form and assigned something like:
@file = params[:resource][:file_tag_name]
then try:
SmarterCSV.process(@file.original_filename)
or with CSV:
CSV.parse(@file.original_filename)
The object that Rails creates in the params hash for a file upload is an instance of a subclass of the IO class, so you need to get to the underlying file to pass it to SmarterCSV.
For more information about how Rails handles file uploading, please see http://guides.rubyonrails.org/form_helpers.html#what-gets-uploaded
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With