0
My program reads a file . txt for example, iterates over the internal information and store in the database.
But at the time the file is sent he gives an error saying he does not know the file path... In my understanding, when the file was sent it was stored in the variable that is specified, but that’s okay.
The error is shown in the attached image. It is noticed that the pointed line is precisely where the file that is not found will be opened.
Controller
def import
errors = []
file = params['file']
File.open(file).each do |line|
begin
line = line.split("\t")
next if line[0] == "Comprador"
buyer = line[0] rescue row[0]
description = line[1] rescue line[1]
unity_price = line[2].to_f rescue line[2].to_f
quantiti = line[3].to_i rescue line[3] .to_i
address = line[4] rescue line[4]
provider = line[5] rescue line[5]
total_gross = "Total: #{(unity_price * quantiti).round(2)}"
Record.create(buyer: buyer, description: description, unity_price: unity_price, quantiti: quantiti, address: address, provider: provider)
rescue Exception => err
errors << err.message
end
end
if errors.banck?
flash[:success] = "Imported with successful"
else
flash[:error] = errors.join(", ")
end
redirect_to "/file"
View
<%= form_tag import_path, method: :post do %>
<div class="input-group no-border">
<%= file_field_tag 'file' %>
<%= button_tag 'submit'%>
</div>
<% end %>
the program hangs on the line "File.open(file). each of |line|"
Show, I forgot about Multipart. About the read method, it was generating another error, as it transformed everything into a string and was passing the whole file as a name or something... After changing the . read to . path it passed the path!
– Amystherdam
Good! In the case of
read
, I meant instead ofFile.open(file)
you can callparams['file'].read
.– Sidney
Ah, yeah! Stay cool
– Amystherdam