Skip to content

Instantly share code, notes, and snippets.

@jescalan
Created June 25, 2012 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jescalan/2988847 to your computer and use it in GitHub Desktop.
Save jescalan/2988847 to your computer and use it in GitHub Desktop.
generate csv
# This is the easiest way to generate a csv and have it downloaded by the user.
# Drop this in a method in the controller and call it on click.
require 'csv'
file = CSV.generate do |csv|
csv << ["Name", "Email"] # headers
Contact.all.each do |person|
csv << [person.name, person.email]
end
end
send_data file, :type => 'text/csv; charset=iso-8859-1; header=present', :disposition => "attachment;filename=filename.csv"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment