Skip to content

Instantly share code, notes, and snippets.

@carlsednaoui
Forked from jescalan/sample.rb
Last active October 6, 2015 12:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carlsednaoui/2992027 to your computer and use it in GitHub Desktop.
Save carlsednaoui/2992027 to your computer and use it in GitHub Desktop.
Easily generate a csv file.
# 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