Roo с rails4 дает неопределенный метод `[] 'для nil: NilClass

Я пытаюсь импортировать файлы CSV и Excel в проект rails 4 (с проверкой), используя гем Roo, основанный наhttp://railscasts.com/episodes/396-importing-csv-and-excel.

Я внес некоторые изменения в учетную запись Rails4 вместо Rails3 и изменений в Roo, и моя модель ProjectImporter теперь выглядит следующим образом:

class ProductImport
  include ActiveModel::Model
  attr_accessor :file

  def initialize(attributes = {})
    attributes.each { |name, value| send("#{name}=", value) }
  end

  def persisted?
    false
  end

  def save
    if imported_products.map(&:valid?).all?
      imported_products.each(&:save!)
      true
    else
      imported_products.each_with_index do |product, index|
        product.errors.full_messages.each do |message|
          errors.add :base, "Row #{index + 2}: #{message}"
        end
      end
      false
    end
  end

  def imported_products
    @imported_products ||= load_imported_products
  end

  def load_imported_products
    spreadsheet = open_spreadsheet
    spreadsheet.default_sheet = spreadsheet.sheets.first
    puts "!!! Spreadsheet: #{spreadsheet}"
    header = spreadsheet.row(1)
    (2..spreadsheet.last_row).map do |i|
      row = Hash[[header, spreadsheet.row(i)].transpose]
      product = Product.find_by(id: row['id']) || Product.new
      product.attributes = row.to_hash.slice(*['name', 'released_on', 'price'])
      product
    end
  end

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

Когда я пытаюсь запустить импорт (используя тестовые данные CSV), происходит сбойheader = spreadsheet.row(1) с ошибкойundefined method '[]' for nil:NilClass, Экстраputs Заявление, которое я включил, подтверждает, чтоspreadsheet само по себе не ноль: это дает!!! Spreadsheet: #<Roo::CSV:0x44c2c98>, Но если я попытаюсь вызвать почти любой из ожидаемых методов, таких как#last_row, это дает мне ту же неопределенную ошибку метода.

Так что я делаю не так?

Ответы на вопрос(1)

Ваш ответ на вопрос