-
-
Save carlsednaoui/2992027 to your computer and use it in GitHub Desktop.
Easily generate a csv file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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