I am trying to import data from a CSV file, I don't want to save the file. I have found a similar question here: Ruby on Rails - Import Data from a CSV file
However, when I try and use this solution I get an error.
My Form is as follows:
Import a CSV file
<%= file_field_tag :file %>
<div class="actions form-group" id="button_text">
<p>
<%= button_tag(type: 'submit', class: "btn btn-primary button-submit") do %>
Submit
<% end %>
On the new page the submission of this form calls the create action, the create action then calls the line:
Product.import(params[:file])
I have the following import method in my model:
def self.import(myfile)
require 'csv'
csv_text = File.read(myfile)
csv = CSV.parse(csv_text, :headers => true)
csv.each do |row|
end
end
where 'myfile' is params[:file].
I am aware I am not doing anything with the data yet. I am getting an error with line csv_text = File.read(myfile)
which is: No such file or directory @ rb_sysopen - X.csv
When looking for a solution I have seen .path is used. If i change this line to csv_text = File.read(myfile.path)
I get the error: undefined method `path' for "X.csv":String
Can anybody help? I feel like I must be very close to a solution but I just keep going round and round between these two errors. Is it possible to get the data from the csv without saving it first?
Thanks for your time