Roo with rails4 giving undefined method `[]' for nil:NilClass

后端 未结 1 793
我在风中等你
我在风中等你 2021-01-21 16:52

I\'m trying to import CSV and Excel files into a rails 4 project (with validation) using the Roo gem, based on http://railscasts.com/episodes/396-importing-csv-and-excel.

<
相关标签:
1条回答
  • 2021-01-21 17:15

    I had the same problem, it seems a problem about file enconding, I used this code and it was fixed.

    def open_spreadsheet
        case File.extname(file.original_filename)
            when ".csv" then Roo::CSV.new(file.path, csv_options: {encoding: "iso-8859-1:utf-8"})
            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
    

    I hope that helps for you.

    0 讨论(0)
提交回复
热议问题