I want to upload a file to my rails webpage, but not store it in the database. I just want to upload it and then get the data out of it and update the database with the information in the uploaded file.

I have a settings controller that I use for most administrative tasks. So this is where I’m going to put the form for the file upload.

View:

<%= form_tag({action: :upload}, multipart: true) do %>
	Upload xml file of abstracts: <%= file_field_tag 'abstracts' %>
<%= submit_tag %> <% end %>

Routes:

resources :settings, only: [:index, :edit, :update] do
    post 'upload', on: :collection
  end

Controller:

def upload
    file_data = params[:abstracts].tempfile
    match_abstracts(file_data)
    flash[:notice] = "File uploaded"
    redirect_to settings_path
  end

Then the method match_abstracts is where I do all the work I want to do on the file. Right now, all I have is the information matching names in the uploaded file with names stored in the database.

def match_abstracts(file_data)
    File.open(file_data) do |file|
      @doc = Nokogiri::XML(file)
      elems = @doc.xpath("//Speaker")
      @applicants = Applicant.without_abstract
      @applicants.each do |applicant|
        elems.each do |e|
          if e.parent.at('AcceptedTrack')
            if ((e.at('FirstName').text == applicant.firstname) && (e.at('FamilyName').text == applicant.lastname))
              logger.info "=="
              logger.info "TITLE: #{e.parent.at('Title').text}"
              logger.info "ABSTRACT: #{e.parent.at('Content').text}"
              logger.info "#{e.at('FirstName').text}"
              logger.info "#{e.at('FamilyName').text}"
            end 
          end
        end
      end
    end
  end

This just prints to the log the information that I’ll need to use to update the database. Next I need to write another method, probably called update_applicant with the commands to add this information to the applicant’s record.