Importing CSV and Excel in rails 4 and ruby 2.0

You can use a single gem 'roo' for reading all types of file, instead of rubyXL, spreadsheet etc. multiple gems for different files(.xls, .xlsx).
You can check [here railscasts][1] for importing csv and excel import in ruby 1.9.3.

But this code snippet is not working with Ruby 2.0.

As [this episode][2] used gem 'roo' for reading files and gem 'roo' has been updated for rails 4.0.

So you need to do some code changes to work [it][3] with rails 4.0

Changes are:

  1. In Method: self.open_spreadsheet(file)
 Csv.new(file.path, nil, :ignore) => Roo::Csv.new(file.path, nil, :ignore)
 Excel.new(file.path, nil, :ignore) => Roo::Excel.new(file.path, nil, :ignore)
 Excelx.new(file.path, nil, :ignore) => Roo::Excelx.new(file.path, nil, :ignore)
  1. Remove
    require 'iconv'
    from config/application.rb

    For more about roo gem [click here][4]
    [1]: http://railscasts.com/episodes/396-importing-csv-and-excel
    [2]: http://railscasts.com/episodes/396-importing-csv-and-excel
    [3]: http://railscasts.com/episodes/396-importing-csv-and-excel
    [4]: http://rubydoc.info/gems/roo/1.12.2/frames

Sunil Sharma

I am a Ruby on Rails developer, passionate about building interesting and innovative web and mobile apps. | Ruby on Rails | Ember.js | Cordova | Travelling | Party | Trekking ...

Read More