0

I am setting up data import based on RailsCast #396, with validation. I am importing Skills using the skills_import.rb model.

The skills_import.rb model is invoked with an actual .csv filename, which should be handled by Roo. But the returned object seems not to exist, and raises an undefined method [] for nil:NilClass error when header = spreadsheet.row(1) is reached.

The skills_import.rb model:

def load_imported_skills
  spreadsheet = open_spreadsheet
  header = spreadsheet.row(1)
  (2..spreadsheet.last_row).map do |i|
    row = Hash[[header, spreadsheet.row(i)].transpose]
    column = Skill.find_by_id(row["id"]) || Skill.new
    column.attributes = row.to_hash.slice(*Skill.accessible_attributes)
    column
  end
end

def open_spreadsheet
  case File.extname(file.original_filename)
  when ".csv" then Roo::CSV.new(file.path, nil, :ignore)
  when ".xls" then Roo::Excel.new(file.path, nil, :ignore)
  when ".xlsx" then Roo::Excelx.new(file.path, nil, :ignore)
  else raise "Unknown file type: #{file.original_filename}"
  end
end

Nevertheless, the class of open_spreadsheet object is Roo::CSV.

Thanks !

user1185081
  • 1,898
  • 2
  • 21
  • 46
  • problem is encoding. duplicate question with answer here https://stackoverflow.com/questions/26657194/roo-with-rails4-giving-undefined-method-for-nilnilclass – John Moses Jun 17 '17 at 14:21
  • Possible duplicate of [Roo with rails4 giving undefined method \`\[\]' for nil:NilClass](https://stackoverflow.com/questions/26657194/roo-with-rails4-giving-undefined-method-for-nilnilclass) – John Moses Jun 17 '17 at 14:21

0 Answers0