0
I have an application in Ruby on Rails whose one of the features is to read a text file with the following extensions "TXT/DAT", for this, I need a simple page (only an input field(file) and a button of Submit) for sending the file, the file itself has several lines that are answers of a proof, the file is like this:
"EEECBEBABABCEEAACDAEDABCCDDBEAEAEAEABBDBACCACDEBBEDCBEDEBBDCCDCDABBEADDBACEDCEABCDBCCECEED"
each letter is the answer to a question, and the table in the database will receive each record in a question/answer row
what I’ve done so far
Route file:
get "pages/envio"
post "pages/envio"
view:
<%= form_tag '/pages/envio' ,multipart: true do %>
<label for="file">File to Upload</label> <%= file_field_tag :file %>
<div><%= submit_tag 'Process' %></div>
<% end %>
controller:
def envio
@file = params[:file]
if(@file.respond_to?(:read))
File.read("#{@file}".to_s, 'r') do |file|
while line = file.gets
@gabarito = line[0..line.rstrip.size].split(//)
end
end
elsif(@file.respond_to?(:path))
File.open(@file) do |file|
@file = file
end
render :envio
end
end
any idea?
Hello Eugenio, your question/problem is not clear.
– Luiz Carvalho
Hi, Luiz, so it’s like this, I have several files in format . txt and/or . dat, I need to send them to the system (via input), when pressing the "send" button, I need the file to be read, line by line (if there is more than one line) and that if it is ok, then I can send them to the database, where there is a table called "feedback" with the question/answer fields
– Eugênio Oliveira
Okay, but you sent that code there and didn’t say what’s wrong with it, or the error or the result.
– Luiz Carvalho
as I said, I need to read this file in txt format and display it in a view, after it is confirmed that it has been read correctly it should be saved in the database
– Eugênio Oliveira
Okay. There’s no way to help. Good luck.
– Luiz Carvalho
See here how the screens should look, so you have an idea Dropbox.com/sh/ndhpvmxe0uiupd8/Aabkrse1n4zpnpjwp_h6d-Nla? dl=0
– Eugênio Oliveira
Add the images in your question so other people can help you :)
– Luiz Carvalho
I’m new to stack overflow and I don’t have enough points for it, so I put it in Dropbox.
– Eugênio Oliveira