1

I am trying to upload a csv file with Rails from Active Admin.

I have a model User, which has columns name(string) and age(integer).

My csv file looks like this:

name,age
"Peter",31
"Susan",30
"John",40

Then I have in my admin/user.rb:

ActiveAdmin.register User do
  permit_params :name, :age

  collection_action :upload_csv do
    render "admin/csv/upload_csv_user"
  end

  collection_action :import_csv_data_user, :method => :post do
    ret_val = CsvDb.update_model_from_csv("user", params[:dump][:file])
    redirect_to :action => :index, :notice => ret_val
  end
end

And in my admin/csv/upload_csv_user.html.erb I have:

<%= form_for(:dump, :url=>{:action=>"import_csv_data_user"}, :html => { :multipart => true }) do |f| %>
  <%= f.file_field :file %>
  <%= f.submit "Submit", confirm: "Are You Sure?" %>
<% end %>

Inside a csv_db.rb file I have:

require 'csv'
module CsvDb

  def self.update_model_from_csv(model_name, file)
    csv_data = CSV.read(file.path)
    columns = csv_data.shift
    model = model_name.classify.constantize
    ret_val = "Data updated successfully!"
    begin
      model.import columns, csv_data, on_duplicate_key_update: columns, validate: false
    rescue Exception => e
      ret_val = e
    end
    ret_val
  end
end

When I try to upload the file I get the following error: Illegal quoting in line 1.

And below it's written:

part "\"name"
parse "\"name,age\""
parts ["\"name", "age\""]
unconverted nil
in_extended_col false
csv []

I checked lots of examples and I can't find the error. Maybe my csv file is formatted incorrectly.

Tom Bom
  • 1,589
  • 4
  • 15
  • 38

1 Answers1

1
require 'csv'
module CsvDb

  def self.update_model_from_csv(model_name, file_params)
    CSV.foreach(file_params.path, headers: true) do |row|
      model = model_name.classify.constantize
      begin
        name = row["name"]
        age = row["age"]
        if model.create(name: name, age: age)
          result = "Imported successfully!"
        end
      rescue Exception => e
        result = e
      end
    end
    result
  end
end

Use this method in controller as: -

#pass model name and params[:file], which is csv uploaded from user end
CsvDb.update_model_from_csv(model_name, params[:file])
Anand
  • 6,457
  • 3
  • 12
  • 26
  • it says `undefined method `path' for nil:NilClass` and it highlights `CSV.foreach(file_params.path, headers: true) do |row|` – Tom Bom Oct 02 '18 at 09:13
  • @Al-josh Can you check in which params you are getting csv uploaded file by user? possibly it may be `params[:dump][:file]` so if it's so then change replace params[:file] with actual file params , hope you get it. – Anand Oct 02 '18 at 09:19
  • yeah, then i'm back to `Illegal quoting in line 1.` :( – Tom Bom Oct 02 '18 at 09:24
  • @Al-josh Which version ruby are you using? – Anand Oct 02 '18 at 10:24
  • https://stackoverflow.com/questions/9864064/csv-read-illegal-quoting-in-line-x please have a look at this link, it can resolve your issue. – Anand Oct 02 '18 at 10:26
  • https://snippets.aktagon.com/snippets/616-how-to-fix-illegal-quoting-in-line-x-when-parsing-csv-with-ruby – Anand Oct 02 '18 at 10:28
  • @Al-josh et me know if these links help you. – Anand Oct 02 '18 at 10:29
  • This is correct, thank you. The code in my question was also correct. My csv file was formatted incorrectly, maybe it's the mac version of excel. – Tom Bom Oct 03 '18 at 08:19