How to generate CSV files in Rails
Posted in CSV, Rails, ror on June 28th, 2007 by Satish Chauhan – 4 CommentsController code :
@customers=CustomerInformation.find(:all)
report = StringIO.new
CSV::Writer.generate(report, ,) do |title|
title << [Id, Title, Job Title, First Name, Last Name]
@customers.each do |c|
title << [c.id, c.title, c.job_title, c.first_name, c.last_name]
end
end
report.rewind
send_data(report.read,:type => text/csv; charset=iso-8859-1; header=present,:filename => report.csv, :disposition =>attachment, :encoding => utf8)
end
View
your view will look like