Read TXT/DAT file and Parsing before saving to database (mysql)

Asked

Viewed 313 times

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.

  • 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

  • Okay, but you sent that code there and didn’t say what’s wrong with it, or the error or the result.

  • 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

  • Okay. There’s no way to help. Good luck.

  • See here how the screens should look, so you have an idea Dropbox.com/sh/ndhpvmxe0uiupd8/Aabkrse1n4zpnpjwp_h6d-Nla? dl=0

  • Add the images in your question so other people can help you :)

  • I’m new to stack overflow and I don’t have enough points for it, so I put it in Dropbox.

Show 3 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.