Bill C. wrote:
I have been using Ruby on Rails for around 6 weeks now (so far I love
it), but I’ve gotten to a point in the application where I need to allow
the user to upload comma deliminated text files that will get loaded
into one of the database tables.
Before I get started doing this, I was wondering anyone knows of any
examples or tutorials that deal with this. I’d rather do it the right
way (if there is one) then try and reinvent the wheel with some hacked
solution. Also, let me know if this is something that might be better
done by sliding back to PHP for this part of my application.
I do this with a multi-step ‘wizard’ in my application. Imported
datasets are typically small, so I can store the data in the session
between steps. As the other posters have suggested, I use CSV::Reader.
It’s fast enough for this quantity of data.
First I pull the data into an array of arrays (rows and columns of the
data). I display this and let the user choose which columns contain the
data they want for particular columns in the database table. This
avoids the user having to follow a particular CSV output order.
Second, I create AR objects for each of the rows the user wants
selected, and display them on a second page. I highlight rows that fail
validation and tell them that these will not be imported.
Finally, all the data is imported.