Saturday, May 31, 2008

3. Import Controller code (FasterCSV)

So now for the real code.


The FasterCSV code I received from emailing James Edward Gray II who was nice enough to get me started on working on this...

I am going to break this code apart in bits in subsequent posts and hopefully get a better understanding about what is going on with comments from others...

Here is my first attempt:

class ImportController < ApplicationController
require 'fastercsv'

def index
end

def csv_import

begin
Record.transaction do
FasterCSV.foreach("csv_test/public/csv/test_copy.csv",
:headers => true)
do |row|
transactions.records.create!(row.to_hash)
end
end

flash[:notice] = "Successfully added."
redirect_to 'index'
rescue
#do something with the error
flash[:error] = "error adding. Please try again."
redirect_to '/import/index'
end

end

end

2 comments:

Akkdio said...

Right now this does not work. Running
>ruby import_controller.rb from the controllers directory gets me this error:


import_controller.rb:13: syntax error, unexpected kDO, expecting kEND
do |row|
^
import_controller.rb:28: syntax error, unexpected kEND, expecting $end


I have tried deleting the end and adding end. I am tired and maybe I can see straight tomorrow.

JEG2 said...

The `do` must be on the same line as the method call it creates a block for. I'm pretty sure that's your syntax error.