4

I am making an application in which it import a csv file names user.csv.But the problem i am facing is that it gives an error

ArgumentError in CsvimportController#import

wrong number of arguments (1 for 0)

And the code of the CsvimportController is

require 'csv'
class CsvimportController < ApplicationController
def import

results = import('anas.csv') do
read_attributes_from_file
end


end
end

And i have also give the specification of csv-mapper and fastercsv in gem file.

Can anyone help me???

Any help would be appreciated..

Thanks

Mohd Anas
  • 634
  • 1
  • 9
  • 22
  • please change the action name and try because import is the function of csv lib so you need to give different name. – Anand Soni Feb 15 '13 at 12:53
  • possible duplicate of [Ruby on Rails - Import Data from a CSV file](http://stackoverflow.com/questions/4410794/ruby-on-rails-import-data-from-a-csv-file) – Chris Ledet May 09 '13 at 02:38

2 Answers2

2

Take a look at Railscast 396 on how to import data from CSV and Excel files.

The smarter_csv project aims to provide a better dealing with CSV files so it would worth to take a look.

Paulo Fidalgo
  • 21,709
  • 7
  • 99
  • 115
0

It's easy if you use the Gem smarter_csv.

All you need to do is this:

 require 'smarter_csv'

 def import(filename)
   results = SmarterCSV.process( filename, options_hash )
 end

and you need to specify the options in the options_hash according to the documentation of smarter_csv

There are tons of useful options, including manipulation of the headers, custom-headers, ignoring columns, and type-conversion of values.

If your CSV file is large, you can also chunk the incoming data for parallel processing.

Tilo
  • 33,354
  • 5
  • 79
  • 106