How to import CSV files into rails?

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

Argument Err in CsvimportController # import

wrong number of arguments (1 to 0)

And the CsvimportController code

require 'csv' class CsvimportController < ApplicationController def import results = import('anas.csv') do read_attributes_from_file end end end 

And I also give the specification of csv-mapper and fastcsv in the gem file.

Can someone help me ???

Any help would be appreciated.

thanks

+4
source share
2 answers

Check out Railscast 396 on how to import data from CSV and Excel files.

The smarter_csv project is designed to provide better interaction with CSV files, so it’s worth a look.

+2
source

This is easy if you use smarter_csv .

All you have to do is the following:

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

and you need to specify parameters in options_hash according to smarter_csv documentation

There are many useful options, including header manipulation, custom headers, column ignoring, and value type conversion.

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

0
source

Source: https://habr.com/ru/post/1444515/


All Articles