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

0

I have an application in Ruby on Rails whose function is to read a text file with the following "TXT / DAT" extensions, so I need a simple page (just an input field (file) and a button of submit) to send the file, the file itself has several lines that are responses of a proof, the file looks like this:

"EEECBEBABABCEEAACDAEDABCCDDBEAEAEAEABBDBACCACDEBBEDCBEDEBBDCCDCDABBEADDBACEDCEABCDBCCECEED"

Each letter is the answer of a question, and the table in the database will receive each record in a line question / answer

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 ideas?

    
asked by anonymous 17.05.2017 / 13:24

0 answers