ruby-on-railsruby-on-rails-3roo-gem

Rails Csv.new where is this class being called from?


I'm creating my own version of Rayan Bates CSV screencast here...

http://railscasts.com/episodes/396-importing-csv-and-excel

I have this in my model...

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

...and am getting this error in my app...

 NameError in StudentsController#import

uninitialized constant Student::Csv

Rails.root: /home/wintas/railsApps/t4
Application Trace | Framework Trace | Full Trace

app/models/student.rb:25:in `open_spreadsheet'
app/models/student.rb:13:in `import'
app/controllers/students_controller.rb:12:in `import'

I can't find where the class 'Csv' is initialized, or where it should be coming from. Any help appreciated.


Solution

  • I believe that since that Railscast was published, Roo has been updated to namespace Csv, Excel, and Excelx under the Roo namespace. Try this instead:

    def self.open_spreadsheet(file)
      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