I was hoping you could help me. I need the ability to upload a large
quantity of data via CSV - via an upload form. The CSV file then needs
parsing and the data entering into the relevant module.
Does anyone have experience of this or perhaps knows of a posting,
generator, existing code or any help at all? I’ve been looking but not
come
across anything so far. I can do this in PHP but would very much like
to
crack this in Rails.
I was hoping you could help me. I need the ability to upload a large
quantity of data via CSV - via an upload form. The CSV file then needs
parsing and the data entering into the relevant module.
Make a form with an upload field. Something like this:
class UploadController < ApplicationController
def parse
if params[:data].size.zero? then
flash[:error].now = ‘Forgot to upload some data’
render ‘index’
return
end
params[:data].rewind # Make sure Tempfile / StringIO is at start of
data
row_count = 0
CSV::Reader.parse(params[:data]) do |row|
row_count += 1
# Do something interesting with row:
# row[0] is first column, row[1] is second column, etc.
end