How to solve error: Unquoted fields do not allow or from csv file

Posted by : on

Category : Rails

Hello Friends, We often use csv upload feature in our rails applications. If you have converted a xls or xlsx file to csv, it might be possible that csv file contains \r\n in it. If you are using rubygem roo, it will raise an exception while parsing csv. To import data from csv you have to modify that csv file. Here is an example of that

Replace file content and remove \r\n

  def filter_csv(file_name)
    csv_text =, :encoding => 'ISO-8859-1')
    #remove blank lines
    csv_text = csv_text.gsub(/(\r\n)/, '')
    @csv = CSV.parse(csv_text, :headers => true, skip_blanks: true)
    # Overwrite csv file
    File.write(file_name, @csv)
  @spreadsheet =
  # Now you can use this @spreadsheet to manipulate data


About Ram Laxman Yadav
Ram Laxman Yadav

Software architect, Full Stack Web Developer, based in NCR, India

Email :

Website :

About Ram Laxman Yadav

Hi, my name is Ram Laxman Yadav. I built this site using devlopr-jekyll :D

Useful Links