gsueagle2008
gsueagle2008

Reputation: 4663

syntax error when running rake task

I am new to ruby and rails, so I am still attempting to get a handle on the syntax and structure of ruby. I am following a tutorial to import a csv file using a task and rake. I keep getting a syntax error though. I am not sure what I am missing, I do not see any difference between the example and my code.

require 'csv'

    desc "Import Voters from CSV File"

    task :import => [:environment] do

      file ="db/my.csv"

      CSV.foreach(file, :headers => true) do |row|
        Voter.create{
          :last_name => row[0]
        }  

      end






(See full trace by running task with --trace)
Erics-MacBook-Air:cloudvoters ecumbee$ rake db:import --trace
rake aborted!
/Users/ecumbee/Desktop/cloudvoters/lib/tasks/import.rake:11: syntax error, unexpected tASSOC, expecting '}'
      :last_name => row[0], 
                   ^
/Users/ecumbee/Desktop/cloudvoters/lib/tasks/import.rake:12: syntax error, unexpected '}', expecting '='
    }  
     ^

Upvotes: 1

Views: 775

Answers (2)

Prathamesh Sonpatki
Prathamesh Sonpatki

Reputation: 906

As @pjam pointed out, you have to use parenthesis before curly braces, or skip both.

For eg.Voter.create :last_name => row[0]

The other problem related to unexpected $end, expecting kEND end is you have a missing end

require 'csv'

desc "Import Voters from CSV File"

task :import => [:environment] do

  file ="db/my.csv"

  CSV.foreach(file, :headers => true) do |row|
    Voter.create{
      :last_name => row[0]
    }  

  end

You are ending CSV.foreach block with the end on last line but not ending the task So by adding extra end this error will be removed.

Hope this helps.

Upvotes: 1

pjam
pjam

Reputation: 6356

You're missing the parenthesis which would surround the hash you're giving as a parameter of the create function

    Voter.create({
      :last_name => row[0]
    })

You can also skip both parenthesis & curly brackets

    Voter.create :last_name => row[0]

Upvotes: 2

Related Questions