Author-icon Posted By : Satish Chauhan @ 29 Jun, 2007 at 01:18PM

How to import CSV file in rails?

The CSV text-file format is a common choice for both import and export when performing data migrations. What if you want to import CSV files within a Rails application?

Here is the code that allow you to upload CSV data onto the MySQL database from the web interface
This code save data direct to database without saving it to temp file

Controller:


require ‘csv’

def csv_import
@parsed_file=CSV::Reader.parse(params[:dump][:file])
n=0
@parsed_file.each do |row|
c=CustomerInformation.new
c.job_title=row[1]
c.first_name=row[2]
c.last_name=row[3]
if c.save
n=n+1
GC.start if n%50==0
end
flash.now[:message]=”CSV Import Successful, #{n} new records added to data base"
end

View:

your view will look like


<% form_for :dump, :url=>{:controller=>”customer_informations”,
:action=>"csv_import"},
:html => { :multipart => true } do |f| -%>
< label for="dump_file" >
Select a CSV File :
</label >
<%= f.file_field :file -%>
<%= submit_tag 'Submit' -%>
<% end -%>

31 Comments to "How to import csv file in rails"
Andy said,
on 15 Nov, 2007 at 12:36PM

hi, I've experienced a problem when importing csv in Rails, ie what if , (comma) is part of the text to be imported, job_title ="Rail Developer, Company IT"..

What's the best way to overcome this as I'm new to Rails ?

Thx
Andy

Hero said,
on 03 Jul, 2008 at 10:18PM
Your comment is awaiting moderation.
Dominic said,
on 03 Jul, 2008 at 10:20PM
Your comment is awaiting moderation.
Dominic said,
on 03 Jul, 2008 at 10:43PM
Your comment is awaiting moderation.
Diesel said,
on 03 Jul, 2008 at 10:49PM
Your comment is awaiting moderation.
Aron said,
on 03 Jul, 2008 at 11:17PM
Your comment is awaiting moderation.
Aron said,
on 03 Jul, 2008 at 11:20PM
Your comment is awaiting moderation.
Aron said,
on 03 Jul, 2008 at 11:58PM
Your comment is awaiting moderation.
Hero said,
on 04 Jul, 2008 at 12:14AM
Your comment is awaiting moderation.
Halo said,
on 04 Jul, 2008 at 12:25AM
Your comment is awaiting moderation.
Joe said,
on 04 Jul, 2008 at 12:33AM
Your comment is awaiting moderation.
Heel said,
on 04 Jul, 2008 at 12:51AM
Your comment is awaiting moderation.
July said,
on 04 Jul, 2008 at 12:53AM
Your comment is awaiting moderation.
Arnie said,
on 04 Jul, 2008 at 12:59AM
Your comment is awaiting moderation.
Neo said,
on 04 Jul, 2008 at 01:29AM
Your comment is awaiting moderation.
Bill said,
on 04 Jul, 2008 at 01:46AM
Your comment is awaiting moderation.
Hero said,
on 04 Jul, 2008 at 01:51AM
Your comment is awaiting moderation.
Aron said,
on 04 Jul, 2008 at 02:05AM
Your comment is awaiting moderation.
Halo said,
on 04 Jul, 2008 at 02:21AM
Your comment is awaiting moderation.
Arnie said,
on 04 Jul, 2008 at 02:52AM
Your comment is awaiting moderation.
Kir said,
on 04 Jul, 2008 at 03:01AM
Your comment is awaiting moderation.
Joe said,
on 04 Jul, 2008 at 03:18AM
Your comment is awaiting moderation.
Bill said,
on 04 Jul, 2008 at 03:41AM
Your comment is awaiting moderation.
Bill said,
on 04 Jul, 2008 at 03:44AM
Your comment is awaiting moderation.
Hero said,
on 04 Jul, 2008 at 04:09AM
Your comment is awaiting moderation.
Kir said,
on 04 Jul, 2008 at 04:22AM
Your comment is awaiting moderation.
Kir said,
on 04 Jul, 2008 at 04:59AM
Your comment is awaiting moderation.
Halo said,
on 04 Jul, 2008 at 05:20AM
Your comment is awaiting moderation.
Bill said,
on 04 Jul, 2008 at 05:20AM
Your comment is awaiting moderation.
Jane said,
on 04 Jul, 2008 at 05:22AM
Your comment is awaiting moderation.
Diesel said,
on 04 Jul, 2008 at 05:34AM
Your comment is awaiting moderation.

Leave a comment

( *required )
( *required )

Sponsored Links