2

image

As you can see from the picture, i have a button to upload a csv file. How can i get the uploaded file to file.read(PATH) so the script takes the file in.

My Controller (calls_controller.rb)

 def index csv_text = File.read(UPLOADED FILE PATH) csv = CSV.parse(csv_text, :headers => true, :encoding => 'ISO-8859-1', :col_sep => ',') ... .. end 

I tried to give my index method an argument

 def index(file) 

but that did not work for me.

This is my view (index.html.erb). I call out the method in the submit_tag, but can i somehow pass an argument or ?.

<div> <h4>Import data!</h4> <%= form_tag import_calls_path, multipart: true do %> <%= file_field_tag :file, required: true %> <%= submit_tag "Import CSV", method: :index%> <% end %> </div> 
2
  • Where is the file stored ? You would need to download the file to some path and then provide the path to the file to File.read Commented Feb 21, 2020 at 12:46
  • I actually have no idea, i have not changed it anywhere, but if thats the case then il try to find it out :) Commented Feb 21, 2020 at 12:54

1 Answer 1

3

When you upload a file with a form like the one in your question then the following should work:

File.read(params[:file].tempfile.path) 

Or even simpler:

params[:file].read 
Sign up to request clarification or add additional context in comments.

2 Comments

csv_text = File.read(params[:file]) Gives me an error "no implicit conversion of nil into String" tried diffrent ways, but non worked. With .read at the end too. Same error
How do the request and the params look like in the log file when sending the form?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.